Page 1 of 2

A good OO lib

Posted: Sun Apr 01, 2012 3:14 pm
by rokit boy
Can someone suggest me a very good OO lib with the syntax as close to this:

Code: Select all

class Class1 {

hi = 1
hi2 = 2 -- general fields

function Class1(as) -- constructor
hi2 = as
end

function getHi() -- methods
return hi
end
Then in another script:

Code: Select all

Class1 class1 = new Class1(12)

trol = class1.getHi()
print(trol)
Can someone suggest me one as close to that as possible? Or any class lib if what I just show is nowhere near possible in lua or any other thing similar to my examples.

Re: A good OO lib

Posted: Sun Apr 01, 2012 3:35 pm
by kikito
I think Slither is your best bet. Here're some examples.

https://bitbucket.org/bartbes/slither/s ... amples.lua

Obviuosly the syntax is not exactly like the one you proposed, but it's near enough.

Re: A good OO lib

Posted: Sun Apr 01, 2012 3:40 pm
by rokit boy
Ok so I think it looks good but when is the __init__ and the __add__ called?
I'm pretty sure __init__ is the initialization but what is __add__?

Re: A good OO lib

Posted: Sun Apr 01, 2012 4:00 pm
by nevon
rokit boy wrote:Ok so I think it looks good but when is the __init__ and the __add__ called?
I'm pretty sure __init__ is the initialization but what is __add__?
__add__ is run when you try to add two instances.

Code: Select all

derpherp = derp + herp
You can look at the examples.

Re: A good OO lib

Posted: Sun Apr 01, 2012 4:06 pm
by rokit boy
nevon wrote:
rokit boy wrote:Ok so I think it looks good but when is the __init__ and the __add__ called?
I'm pretty sure __init__ is the initialization but what is __add__?
__add__ is run when you try to add two instances.

Code: Select all

derpherp = derp + herp
You can look at the examples.
Thanks!

Re: A good OO lib

Posted: Sun Apr 01, 2012 4:07 pm
by rokit boy
are there any other functions apart from __init__ and __add__ ?

Re: A good OO lib

Posted: Sun Apr 01, 2012 5:53 pm
by nevon
rokit boy wrote:are there any other functions apart from __init__ and __add__ ?
Yes. __call__, __len__, __add__, __sub__, __mul__, __div__, __mod__, __pow__, and __neg__

They're all just aliases of the Lua standard metamethods.

Re: A good OO lib

Posted: Sun Apr 01, 2012 6:26 pm
by rokit boy
Sow ill init be the constructor, __add,div,sub,mul,mod,pow,neg__ are mathematical and the rest I don't know.

Looking forward to making stuff with this.

Re: A good OO lib

Posted: Sun Apr 01, 2012 7:44 pm
by rokit boy
What is __len__?

Btw. I smell python. Is it python inspired?

Re: A good OO lib

Posted: Sun Apr 01, 2012 7:52 pm
by Robin
rokit boy wrote:What is __len__?
The # operator. It is not overridable for tables in Lua 5.1 (though it will be for Lua 5.2).
rokit boy wrote:Btw. I smell python. Is it python inspired?
Yes.