32 lines of goodness, yet another OO lib

Showcase your libraries, tools and other projects that help your fellow love users.
{coder}
Prole
Posts: 11
Joined: Thu Jan 05, 2012 3:43 am

Re: 32 lines of goodness, yet another OO lib

Post 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?
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: 32 lines of goodness, yet another OO lib

Post 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.
{coder}
Prole
Posts: 11
Joined: Thu Jan 05, 2012 3:43 am

Re: 32 lines of goodness, yet another OO lib

Post 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?
User avatar
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

Post 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:
  1. When there is 1 argument and that is a literal string
  2. 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)
{coder}
Prole
Posts: 11
Joined: Thu Jan 05, 2012 3:43 am

Re: 32 lines of goodness, yet another OO lib

Post by {coder} »

thank that help clear thigs up alot. basicaly each function returns a value and the next calls a member of it
User avatar
Borg
Prole
Posts: 21
Joined: Fri Mar 02, 2012 10:59 am

Re: 32 lines of goodness, yet another OO lib

Post 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
User avatar
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

Post 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!
User avatar
Borg
Prole
Posts: 21
Joined: Fri Mar 02, 2012 10:59 am

Re: 32 lines of goodness, yet another OO lib

Post 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;
}
User avatar
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

Post by ishkabible »

29! that's prime!!! what happened to some nice powers of 2?
User avatar
juno
Citizen
Posts: 85
Joined: Thu May 10, 2012 4:32 pm
Location: London

Re: 32 lines of goodness, yet another OO lib

Post 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
wat ya mean she's in another castle!?
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests