views:

165

answers:

2

For example,

function test (a)
    name = nameof(a)
    print(name)
end

test(def) --should print "def"

Are there any lua tricks to implement something similar to the above?

+1  A: 

Try using the debug library.

You can use debug.getlocal ([thread,] level, local) to get information about a local variable, including its name.

Karl Voigtland
It's semi usable. The "local" in debug.getlocal ([thread,] level, local) is actually the index into an array of variables declared local in the stack denoted by level.
jameszhao00
Yeah, you're right. Its not really the general purpose solution you are looking for.
Karl Voigtland
Don't forget that the `debug` module is strongly discouraged in production code that is not a debugger.
RBerteig
A: 

What you asking for is not possible in pure Lua.

If you really need this, try fiddling with Metalua.

Alexander Gladysh