32 lines of goodness, yet another OO lib
Re: 32 lines of goodness, yet another OO lib
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?
code works? and how it allows this syntax?
Re: 32 lines of goodness, yet another OO lib
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
I understand(mostly) how to make classes I just don't get how this syntex works
i.e.
what functions are clalled when you do this?
i.e.
Code: Select all
class "ClassName" : extends(BaseClassName) {
memberName = nonNilValue;
}
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: 32 lines of goodness, yet another OO lib
It's a clever bit of lua syntax abuse, the actual call is more like this:
Lua allows you to drop the parentheses on two occasions:
Using that we can split the above up:
Code: Select all
class("ClassName"):extends(BaseClassName)({ memberName = nonNilValue })
- When there is 1 argument and that is a literal string
- When there is 1 argument and that is a literal table
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
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
Hello.
Here is slighty improved version, I tested it briefly and it seems to work fine.
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
- ishkabible
- Party member
- Posts: 241
- Joined: Sat Oct 23, 2010 7:34 pm
- Location: Kansas USA
Re: 32 lines of goodness, yet another OO lib
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
Okey, final version.. totaly self contained.
I like it best, no external definitions.
Now its time to start writing some games for LOVE :)
Now you can declare classes like that:
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
Code: Select all
class "SomeClass"
class "SomeClass2" {
a=1;
b=2;
}
class "ExtClass1" : extends(SomeClass)
class "ExtClass2" : extends(SomeClass2) {
x=5;
y=6;
}
- ishkabible
- Party member
- Posts: 241
- Joined: Sat Oct 23, 2010 7:34 pm
- Location: Kansas USA
Re: 32 lines of goodness, yet another OO lib
29! that's prime!!! what happened to some nice powers of 2?
Re: 32 lines of goodness, yet another OO lib
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.
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:
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
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
wat ya mean she's in another castle!?
Who is online
Users browsing this forum: No registered users and 3 guests