By this I mean Lua already has Object oriented Programming, especially using Roblox's ModuleScripts. Is it possible to create an Interface, much like Java, so objects can implement undefined functions?
Is it possible to create a code Interface?
2 answers
It is possible. Here's a Roblox wiki article on OOP: http://wiki.roblox.com/index.php?title=OOP
For inheritance, define a class instantiation function in terms of a superior class. Here's an example:
vector={
new=function(...)
local components={...}
local length=#components
return setmetatable({class="vector"},{
__index=function(t,k)
if type(k)=="number"then
return components[k]
end
end,
__newindex=function(t,k,v)
if type(k)=="number"then
if type(v)=="number"then
components[k]=v
return
else
error("Must be a number")
end
end
error("Cannot set "..k)
end,
__add=function(a,b)
if a.class==b.class and a.length==b.length then
local t={}
for i=1,#a do
t[i]=a[i]+b[i]
end
return vector.new(unpack(t))
end
end,
__sub=function(a,b)
if a.class==b.class and a.length==b.length then
local t={}
for i=1,#a do
t[i]=a[i]-b[i]
end
return vector.new(unpack(t))
end
end,
__tostring=function()
return table.concat(components,", ")
end
})
end
}
vector3={
new=function(x,y,z)
local object=vector.new(x,y,z)
rawset(object,"magnitude",math.sqrt(x*x+y*y+z*z))
rawset(object,"class","vector3")
local meta=getmetatable(object)
meta.__add=function(a,b)
if a.class==b.class then
return vector3.new(a.x+b.x,a.y+b.y,a.z+b.z)
end
end
meta.__sub=function(a,b)
if a.class==b.class then
return vector3.new(a.x-b.x,a.y-b.y,a.z-b.z)
end
end
meta.__index=function(t,k)
if k=="x"then return x
elseif k=="y"then return y
elseif k=="z"then return z
end
end
local old=meta.__newindex
meta.__newindex=function(t,k,v)
if type(v)=="number"then
if k=="x"or k=="y"or k=="z"then
if k=="x"then
x=v
elseif k=="y"then
y=v
elseif k=="z"then
z=v
end
rawset(object,"magnitude",math.sqrt(x*x+y*y+z*z))
return
end
end
return old(t,k,v)
end
return object
end
}
local v=vector3.new(12,23,34)
print(v.x)
local a=vector3.new(23,12,1)
local b=v+a
print(b)
5 comments
If you want to create a userdata object like Roblox, you can use newproxy, but it's been deprecated since the creation of Lua 5.1 (which roblox uses.)
Yeah I believe the link you posted also uses newProxy in order to create objects
But I need to ask, does this work with private variables (AKA, variables bound to the .new function)? I
Because if I don't use private variables, using your above script, I can do some stupid thing like v.magnitude = "YOLO" and magnitude's value is overriden with YOLO
If you store variables inside the function and not in the table and you set __newindex so variables can't be set, that would work.
Without the help of an external library (or writing your own if you're so inclined), no this isn't possible. There are libraries in Lua that do make this possible, but it is not a feature of Lua. Lua is not intended to be object oriented.
3 comments
Lua has metatables and creatable userdata. It is possible.
@1waffle1 Right, I didn't say it wasn't possible, I said you'd have to write your own implementation for it, or use somebody else's implementation, which is completely accurate. Object oriented programming/inheritance is not feature of Lua, but you can simulate it.
When I said external libraries, I meant something like SECL ( https://github.com/bartbes/love-misc-libs/blob/master/SECL/full.lua )
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
could you provide a hypothetical example?
what do you mean by implement undefined functions?
Take a look at roblox's Part. It inherits all properties of FormFactorPart, which inherits properties of BasePart, which inherits PVInstance, Which inherits Instance.
Do you mean creating objects in plain Lua, or do you mean changing actual Roblox objects? because Roblox doesn't let you change (or access) any of the already existing ones.
I mean creating objects with inheritance in Lua.
look at this: http://www.roblox.com/OO-Class-Library-v2-2-item?id=142480881