lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Nov 19, 2013, at 5:07 AM, Rena <hyperhacker@gmail.com> wrote:

> 
> This looks like a misunderstanding of table indexing. tricktable.a is equal to tricktable['a'] - you're already creating a string here; it has nothing to do with any variable named a. You could change that name to anything else and it'll come back the same. 
> 
 
Yes of course but if strict is on and I want to use the

 something = a_global_if_it_exists or the_default_value

 construction, I would need to write 

something = rawget(_G['a_global_if_it_exists']) or the_default_value

I was hoping to write a simple function that let strict know I don't want to throw an error if I test for a possibly non existent global so I could write:

something = ifexists(a_global_if_it_exists) or  default_value 

and get the global if it existed or the default value.

What I take from this is the only way to do this is to call as a string so:

function ifexists(g) return rawget(_G[g]) end

something = ifexists('a_global_if_it_exists') or default_value

you are correct that at any point in the code I know what the name of a variable is and can get to it w/o a trip through __index, I guess my objection is aesthetic it seems like there should be a way to ask a variable its name. As you point out I can go from a string to a table member with t['a'], it just seems like I should be able to go the other way.