lua-users home
lua-l archive

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


On Thu, Oct 8, 2009 at 6:44 PM, Duncan Cross wrote:
> On Thu, Oct 8, 2009 at 11:20 PM, Norbert Kiesel wrote:
>> is it possible to get the name of a variable within Lua if I only have
>> the variable?  I want to write a small helper function logger that
>> basically prints the name and the value of the variable
> I don't think there is any way for logger() to determine that - all it
> has is the evaluated value of foo, not some reference to the token
> 'foo'.
> If you don't mind it being logger('foo') instead of logger(foo), that's doable:

With lhf's lbci byte code inspection library [1] and the debug
library, we can do this:

  http://lua-users.org/wiki/GettingVariablesFromValues

That leads to some fun things like supporting pass-by-reference
calling semantics in Lua :)

  local a = 10; local b = 11
  print(a,b) --> 10,11
  swap(a,b)
  print(a,b) --> 11,10

[1] http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lbci