7

I have lua function which accept the arguments. I want to get the name of the argument so that I can log the argument along with error.

Any idea how to get argument name in lua?

  • How come you don't know parameter name inside the function? If you don't, how you refer to them? – Vladislav Rastrusny Mar 23 '10 at 09:20
  • He probably doesn't want to repeat himself in the method arguments and in the logging, e.g. function f(firstname) print("firstname is " .. firstname)... When someone switches the variable name it might not get switched in the string literal. – ponzao Mar 23 '10 at 13:35

1 Answers1

10

Use debug.getlocal.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • Very cool didn't know of this. Deleting my answer because I was wrong. – ponzao Mar 23 '10 at 11:46
  • Can you be more specific? If I have `local foo, bar = 2, 3; PrintVariable(bar, foo)`, how can I print variable name along with its value in function `PrintVariable()`? – jdhao Nov 30 '22 at 09:04
  • @jdhao, see https://stackoverflow.com/questions/65743935/lua-how-to-use-debug-getlocal – lhf Nov 30 '22 at 10:23