jameszhao00
jameszhao00

Reputation: 7301

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;

Upvotes: 17

Views: 12602

Answers (3)

jdhao
jdhao

Reputation: 28329

Now you can also use icecream-lua to get the literal name of parameters:

local icecream = require('icecream')

local foo = 1
local bar = 'abc'

icecream(foo, bar)

The output looks like this:

ic| foo = 1, bar = abc

Upvotes: 1

Alexander Gladysh
Alexander Gladysh

Reputation: 41383

What you asking for is not possible in pure Lua.

If you really need this, try fiddling with Metalua.

Upvotes: 7

Karl Voigtland
Karl Voigtland

Reputation: 7705

Try using the debug library.

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

Upvotes: 2

Related Questions