Page 5 of 6
Re: 32 lines of goodness, yet another OO lib
Posted: Mon Jan 09, 2012 5:39 am
by {coder}
Hi I'm kinda new to lua and was trying to make my own oo lib. can anyone help me understand how allows this
code works? and how it allows this syntax?
Re: 32 lines of goodness, yet another OO lib
Posted: Mon Jan 09, 2012 5:56 am
by MarekkPie
I haven't used his library, but for making one-off classes,
Programming in Lua is the best resource I've found. Chapter 15 (which is what I linked to) and Chapter 16 deal with the OOP-side of Lua.
Re: 32 lines of goodness, yet another OO lib
Posted: Mon Jan 09, 2012 3:09 pm
by {coder}
I understand(mostly) how to make classes I just don't get how this syntex works
i.e.
Code: Select all
class "ClassName" : extends(BaseClassName) {
memberName = nonNilValue;
}
what functions are clalled when you do this?
Re: 32 lines of goodness, yet another OO lib
Posted: Mon Jan 09, 2012 3:19 pm
by bartbes
It's a clever bit of lua syntax abuse, the actual call is more like this:
Code: Select all
class("ClassName"):extends(BaseClassName)({ memberName = nonNilValue })
Lua allows you to drop the parentheses on two occasions:
- When there is 1 argument and that is a literal string
- When there is 1 argument and that is a literal table
So, class "Classname" is class("ClassName"), which returns a table that has a function named "extends", which returns another function that gets called with a table that is the actual definition.
Using that we can split the above up:
Code: Select all
local my_definition = { memberName = nonNilValue }
local class_t = class("ClassName")
local class_f = class_t:extends(BaseClassName)
class_f(my_definition)
Re: 32 lines of goodness, yet another OO lib
Posted: Mon Jan 09, 2012 3:36 pm
by {coder}
thank that help clear thigs up alot. basicaly each function returns a value and the next calls a member of it
Re: 32 lines of goodness, yet another OO lib
Posted: Fri Mar 02, 2012 11:02 am
by Borg
Hello.
Here is slighty improved version, I tested it briefly and it seems to work fine.
Code: Select all
local function define(class,members)
for k,v in pairs(members) do
class.__members[k]=v
end
function class:new(...)
local newclass={}
for k,v in pairs(class.__members) do
newclass[k]=v
end
setmetatable(newclass,{__index=class})
if newclass.initialize then
newclass:initialize(...)
end
return newclass
end
end
function class(name)
local newclass={}
_G[name]=newclass
newclass.__members={}
function newclass:extends(base)
newclass.super=base
for k,v in pairs(base.__members) do
newclass.__members[k]=v
end
return setmetatable(newclass,{__index=base,__call=define})
end
return setmetatable(newclass,{__call=define})
end
Re: 32 lines of goodness, yet another OO lib
Posted: Fri Mar 02, 2012 5:40 pm
by ishkabible
but now it's not a nice round number
you'd think we did math in base 10 with that kind of code!
Re: 32 lines of goodness, yet another OO lib
Posted: Fri Mar 02, 2012 7:54 pm
by Borg
Okey, final version.. totaly self contained.
I like it best, no external definitions.
Now its time to start writing some games for LOVE :)
Code: Select all
function class(name)
local newclass={}
_G[name]=newclass
newclass.__members={}
function newclass.define(class,members)
for k,v in pairs(members) do
class.__members[k]=v
end
end
function newclass.extends(class,base)
class.super=base
for k,v in pairs(base.__members) do
class.__members[k]=v
end
return setmetatable(class,{__index=base,__call=class.define})
end
function newclass.new(class,...)
local object={}
for k,v in pairs(class.__members) do
object[k]=v
end
setmetatable(object,{__index=class})
if object.initialize then
object:initialize(...)
end
return object
end
return setmetatable(newclass,{__call=newclass.define})
end
Now you can declare classes like that:
Code: Select all
class "SomeClass"
class "SomeClass2" {
a=1;
b=2;
}
class "ExtClass1" : extends(SomeClass)
class "ExtClass2" : extends(SomeClass2) {
x=5;
y=6;
}
Re: 32 lines of goodness, yet another OO lib
Posted: Fri Mar 02, 2012 10:32 pm
by ishkabible
29! that's prime!!! what happened to some nice powers of 2?
Re: 32 lines of goodness, yet another OO lib
Posted: Fri May 11, 2012 11:17 pm
by juno
32log is a great piece of code having never touched lua before. It makes things so much easier.
I'm having trouble freeing up space in memory when I dereference my subclasses though. If i create a large table full of instances of my superclass ie.
Code: Select all
for x=0, 50 do
table.insert(table1, Entity:new(x,0))
end
and then execute: table1 = { } collectgarbage()
The dereferenced objects are collected by the garbage collector.
but if I do the same with a subclass of Entity, the gc wont collect it.
Does anyone know a way to solve this?
Here is how i declare my subclass:
Code: Select all
class "Panel" : extends(Entity) { }
function Panel:__init(x, y, w, h, r, s)
self.name="Panel"
self.type=""
....
end
function Panel:draw()
...
end