Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua: Get the literal name of the parameter

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?


Not that anyone needs to explain why they want to do something; some people get grumpy if they aren't given a real-life example. So:

local function registerTestSuite(suite)
   if (LUnit) then
      LUnit:AddTestSuite(
            HotNReady.."_"..GetVariableName(suite), --HotNReady_PizzaTestSuite
            suite);
   end;
end;
like image 237
jameszhao00 Avatar asked Sep 13 '09 04:09

jameszhao00


2 Answers

What you asking for is not possible in pure Lua.

If you really need this, try fiddling with Metalua.

like image 104
Alexander Gladysh Avatar answered Oct 13 '22 23:10

Alexander Gladysh


Try using the debug library.

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

like image 1
Karl Voigtland Avatar answered Oct 14 '22 01:10

Karl Voigtland