Hm, don’t know if this is for beginners… Anyway, is there a concept like static vs. nonstatic (like Java etc. have it) in luabind lua classes? If so, how do I write this, syntactically?
I thought I would write a static method like this:
function MyClass:myLittleStaticMethod(params)
end
vs. a nonstatic one:
function myLittleNonstaticMethod(params)
end
but turns out to be wrong…
Anyone?
(I’d love to see some real documentation on these things. I mean real stuff, not some very basic examples.
There are more questions: what about abstract classes, interfaces, … How do I do all this in LUA?)
The colon is just a shortcut. These two functions are identical
t = {};
function t:foo(bar) end
function t.foo(self, bar) end
When you call a function using the colon syntax, you implicitly supply the table left of the colon as the first argument to the function. The following two function calls are identical:
t:foo("baz");
t.foo(t, "baz");
So you’re not going to have any luck creating a static method with it.
Thanks, I see. I didn’t imagine LuaBind ‘Lua Classes’ are that rudimentary…
This explains why I couldn’t find any information - because what I was looking for plainly doesn’t exist.
Ok, seems I need to have a closer look at those tables.
Man, in the mid 90’s it had been so hard (for me) to understand OOP, today it’s hard to do anything decent without it.
local FAKECLASS = {}
FAKECLASS.public_var = "public value"
private_var = "private value"
function FAKECLASS.public_func()
print("public function called...")
private_func()
end
function private_func()
print("private function called...")
print(private_var)
end
return FAKECLASS
Save as FakeClassTest.lua:
local fake_class = require("FakeClass")
rprint(fake_class)
fake_class.public_func()
print(fake_class.public_var)
Output looks similar to this:
[public_func] => function: 00000000092EFAC0
[public_var] => public value
public function called...
private function called...
private value
public value
Static functions and properties in Renoises classes are possible too:
-----------------------------------------------------------------------
class "MyClass"
MyClass.message = ""
function MyClass:__init()
self.message = ""
end
function MyClass:print()
print(self.message)
end
function MyClass.sprint()
print(MyClass.message)
end
-----------------------------------------------------------------------
obj1 = MyClass()
obj1.message = "foo"
obj2 = MyClass()
obj2.message = "bar"
MyClass.message = "muh"
obj1:print() --> "foo"
obj2:print() --> "bar"
obj1.sprint() --> "muh"
obj2.sprint() --> "muh"
MyClass, the class definition, is simply a table, so you can add any properties to it as well.
There is no such thing like “abstract classes” in Lua, because Lua is dynamically typed. The compiler does not, actually can not enforce an object to implement a specific interface. But you can check types at runtime and inherit classes from other classes, and thus also mimic interface and strict typing behavior in various ways.
So an interface in Lua is more a “convention” which is applied or tested during runtime.
The more Lua native approach to OO actually would be prototype based programming.
But how exactly you want to use Lua is up to you, depends on which OO approaches you like, and probably more important: which ones you are used to.