2

In JavaScript, you can ask a function object how many arguments does it expect. Is the same thing possible in Lua?

hjpotter92
  • 75,209
  • 33
  • 136
  • 171
Septagram
  • 8,922
  • 12
  • 49
  • 79
  • For a function implemented in Lua, you can also get a "signature" of sorts. See my [answer](http://stackoverflow.com/a/24216007/2226988) to [Is there a way to determine the signature of a Lua function?](http://stackoverflow.com/q/142417/2226988). – Tom Blodget Jul 10 '14 at 02:39

2 Answers2

4

If you're using Lua 5.2, you can use debug.getinfo(f).nparams.

(answer edited in light of Luiz's answer, previous answer was correct only for Lua 5.1)

Mud
  • 26,791
  • 11
  • 57
  • 87
2

You can do it using the debug library:

print(debug.getinfo(f).nparams)
lhf
  • 67,570
  • 9
  • 102
  • 136