Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help,
read this .
rokit boy
Party member
Posts: 198 Joined: Wed Jan 18, 2012 7:40 pm
Post
by rokit boy » Tue Apr 03, 2012 11:31 pm
So I have been working with OOP using metatables and I scribbled out this:
Code: Select all
Letters = {}
newLetter = {}
setmetatable(newLetter,{ __call = function(table,character,upper)
local letter = {actualChar = character, uppercase = upper}
return letter
})
So the syntax is very oop like.
I came across one obstacle.
I want to add it to the Letters table, but for the same name I gave it.
So if I have:
How can I make it so it will be accessible through the Letters table like this:
u wot m8
Xgoff
Party member
Posts: 211 Joined: Fri Nov 19, 2010 4:20 am
Post
by Xgoff » Wed Apr 04, 2012 12:04 am
rokit boy wrote: So I have been working with OOP using metatables and I scribbled out this:
Code: Select all
Letters = {}
newLetter = {}
setmetatable(newLetter,{ __call = function(table,character,upper)
local letter = {actualChar = character, uppercase = upper}
return letter
})
So the syntax is very oop like.
I came across one obstacle.
I want to add it to the Letters table, but for the same name I gave it.
So if I have:
How can I make it so it will be accessible through the Letters table like this:
Code: Select all
Letters = {}
newLetter = {}
setmetatable(newLetter,{ __call = function(table,character,upper)
local letter = {actualChar = character, uppercase = upper}
Letters[character] = letter
return letter
end
})
rokit boy
Party member
Posts: 198 Joined: Wed Jan 18, 2012 7:40 pm
Post
by rokit boy » Wed Apr 04, 2012 12:38 am
...
Did you listen to me?
What if I do:
and I want it to be:
u wot m8
Xgoff
Party member
Posts: 211 Joined: Fri Nov 19, 2010 4:20 am
Post
by Xgoff » Wed Apr 04, 2012 1:25 am
rokit boy wrote: ...
Did you listen to me?
What if I do:
and I want it to be:
if you want to do this then you need to have __newindex set on the environment (you will really want a separate env for this). the Letter function is going to need some way of indicating that the table it returns is special, so that the environment's __newindex can handle them specially
as an example, but using the global environment:
http://codepad.org/Micbz3xO
btw, this won't work if you're assigning to locals
Inny
Party member
Posts: 652 Joined: Fri Jan 30, 2009 3:41 am
Location: New York
Post
by Inny » Wed Apr 04, 2012 5:20 am
You can place a metatable on Letters and change the syntax slightly
Code: Select all
Letters = setmetatable({}, {
__index = function(T, k, v)
return function( something, whatever )
rawset(T, k, { something, whatever } ) -- your code here
return rawget( T, k )
end
})
Letters.Asterisk( "*", true )
print( Letters.Asterisk )
local grave = Letters.Grave( "`", false )
print( grave, Letters.Grave )
trubblegum
Party member
Posts: 192 Joined: Wed Feb 22, 2012 10:40 pm
Post
by trubblegum » Wed Apr 04, 2012 5:24 am
Assigning globals in a constructor is bad.
Code: Select all
letters = {}
letter = {}
letter.new = function(this, character) return setmetatable({character = character}, {__index = this}) end -- presumably
setmetatable(letter, {__call = letter.new})
letters.asterisk = letter('*')
rokit boy
Party member
Posts: 198 Joined: Wed Jan 18, 2012 7:40 pm
Post
by rokit boy » Wed Apr 04, 2012 11:48 am
Thanks people!
u wot m8
Users browsing this forum: No registered users and 212 guests