Metatables, getting the name of the value you set.

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.
Post Reply
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Metatables, getting the name of the value you set.

Post by rokit boy »

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:

Code: Select all

A = Letter("A",true)
How can I make it so it will be accessible through the Letters table like this:

Code: Select all

Letters.A
u wot m8
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Metatables, getting the name of the value you set.

Post by Xgoff »

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:

Code: Select all

A = Letter("A",true)
How can I make it so it will be accessible through the Letters table like this:

Code: Select all

Letters.A

Code: Select all

Letters = {}

newLetter = {}

setmetatable(newLetter,{ __call = function(table,character,upper)
    local letter = {actualChar = character, uppercase = upper}
    Letters[character] = letter
    return letter
end
})
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Metatables, getting the name of the value you set.

Post by rokit boy »

...

Did you listen to me?

What if I do:

Code: Select all

Asterisk = Letter("*",false)
and I want it to be:

Code: Select all

Letters.Asterisk
u wot m8
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Metatables, getting the name of the value you set.

Post by Xgoff »

rokit boy wrote:...

Did you listen to me?

What if I do:

Code: Select all

Asterisk = Letter("*",false)
and I want it to be:

Code: Select all

Letters.Asterisk
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
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Metatables, getting the name of the value you set.

Post by Inny »

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 )
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Metatables, getting the name of the value you set.

Post by trubblegum »

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('*')
User avatar
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

Re: Metatables, getting the name of the value you set.

Post by rokit boy »

Thanks people!
u wot m8
Post Reply

Who is online

Users browsing this forum: No registered users and 212 guests