Modules as basic (lazy) 'classes'

This was bugging me, so I hope the following helps someone (I am relatively new to Corona/LUA so this may not come as news to most people on the forum, but this one had me stumped for a while).

I start with a basic module ‘person.lua’ that I want to use as a class:

-- person.lua  
function new(name)  
print("New person named " .. name);  
this = { name = name }  
function this:hello() print("Hello, my name is " .. this.name); end  
return this  

That looks sound, so now I create multiple ‘instances’ of the above ‘class’ and put them in a table:

-- main.lua  
local people = {}  
table.insert(people, 1, require('person.lua').new('Andrew'))  
-- New person named Andrew  
table.insert(people, 2, require('person.lua').new('Catherine'))  
-- New person named Catherine  
table.insert(people, 3, require('person.lua').new('Henry'))  
-- New person named Henry  

So far so good, but then I call on them:

-- main.lua  
people[1]:hello();  
-- Hello, my name is Henry  
people[2]:hello();  
-- Hello, my name is Henry  

Huh?

Turns out I need to forward reference all of my ‘class’ properties:

-- person.lua  
function new(name)  
print("New person named " .. name);  
this = {}  
local name = name  
function this:hello() print("Hello, my name is " .. name); end  
return this  

This seems bizarre, because while the function definitions are attached to the returned table, the values in this are not. No matter how many times I instantiate ‘person.lua’, this.name will always be equal to the value set in the latest new call.
[import]uid: 66490 topic_id: 30853 reply_id: 330853[/import]

Hi,

Well, this won’t work because actually, emulating classes in Lua is a bit tricky than that.
But considering the fact you’re starting with Lua, I highly recommend you to go though some lectures and links, when you have the time. It’ll help, for sure: you have PIL ( a very good book), Lua-Users, the unofficial Lua faq and the reference manual.

Now, coming back on the problem, what you have to do is set your Person class first.
Actually, I hate polluting the global env with global variables. It is considered a good habit to use locals whenever possible. So i’ll make the Person class local within its chunk, then return its public interface at the end of the file:

--person.lua  
local Person = {}  
Person.\_\_index = Person  
  
function Person:new(name)  
 print("New person named " .. name)  
 local newPerson = {name = name}  
 return setmetatable(newPerson,Person)  
end  
  
function Person:hello()  
 print("Hello, my name is " .. self.name)  
end  
  
return Person  

Note hat, in Lua, the extra parameter which tells a method on which object it has to operate is self, by default, not this (as in Java, for instance).

Now, in the main file, creating new instances is easy:

-- main.lua  
-- Please notice that using require, you don't have to add the ".lua" part in the file name.  
local Person = require 'person'  
local people = {}  
table.insert(people,Person:new('Andrew'))  
table.insert(people,Person:new('Catherine'))  
table.insert(people,Person:new('Henry'))  
-- As you like  

For more information on how classes work in lua, take a look at chapter13 and 16 of PIL.
Hope that helps. In case something is not clear, feel free to ask. [import]uid: 142361 topic_id: 30853 reply_id: 123422[/import]

Thanks Roland - your class example will come in handy. I guess I was just a little confused by the use of the module variable this. Coming from other languages I had just assumed that it referenced the instance, however it is actually reset for each new call.

Guess I better do some more reading!

Thanks again.
Chris. [import]uid: 66490 topic_id: 30853 reply_id: 123456[/import]

No problem.
I’d also like to mention that if you’re working on something that requires to create multiple classes and handle relastionships between them, i’ve wrote a handy tool for that, that you can find on github.
See 30log.
Though it doesn’t prevent you from taking the time to read and understand how classes work in Lua. [import]uid: 142361 topic_id: 30853 reply_id: 123507[/import]

Hi,

Well, this won’t work because actually, emulating classes in Lua is a bit tricky than that.
But considering the fact you’re starting with Lua, I highly recommend you to go though some lectures and links, when you have the time. It’ll help, for sure: you have PIL ( a very good book), Lua-Users, the unofficial Lua faq and the reference manual.

Now, coming back on the problem, what you have to do is set your Person class first.
Actually, I hate polluting the global env with global variables. It is considered a good habit to use locals whenever possible. So i’ll make the Person class local within its chunk, then return its public interface at the end of the file:

--person.lua  
local Person = {}  
Person.\_\_index = Person  
  
function Person:new(name)  
 print("New person named " .. name)  
 local newPerson = {name = name}  
 return setmetatable(newPerson,Person)  
end  
  
function Person:hello()  
 print("Hello, my name is " .. self.name)  
end  
  
return Person  

Note hat, in Lua, the extra parameter which tells a method on which object it has to operate is self, by default, not this (as in Java, for instance).

Now, in the main file, creating new instances is easy:

-- main.lua  
-- Please notice that using require, you don't have to add the ".lua" part in the file name.  
local Person = require 'person'  
local people = {}  
table.insert(people,Person:new('Andrew'))  
table.insert(people,Person:new('Catherine'))  
table.insert(people,Person:new('Henry'))  
-- As you like  

For more information on how classes work in lua, take a look at chapter13 and 16 of PIL.
Hope that helps. In case something is not clear, feel free to ask. [import]uid: 142361 topic_id: 30853 reply_id: 123422[/import]

Thanks Roland - your class example will come in handy. I guess I was just a little confused by the use of the module variable this. Coming from other languages I had just assumed that it referenced the instance, however it is actually reset for each new call.

Guess I better do some more reading!

Thanks again.
Chris. [import]uid: 66490 topic_id: 30853 reply_id: 123456[/import]

No problem.
I’d also like to mention that if you’re working on something that requires to create multiple classes and handle relastionships between them, i’ve wrote a handy tool for that, that you can find on github.
See 30log.
Though it doesn’t prevent you from taking the time to read and understand how classes work in Lua. [import]uid: 142361 topic_id: 30853 reply_id: 123507[/import]

Great thread here! Very helpful for trying to get my head around trying to emulate some degree of OOP in lua. Thanks :slight_smile: [import]uid: 105707 topic_id: 30853 reply_id: 126695[/import]

Great thread here! Very helpful for trying to get my head around trying to emulate some degree of OOP in lua. Thanks :slight_smile: [import]uid: 105707 topic_id: 30853 reply_id: 126695[/import]