2

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

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Septagram
  • 9,425
  • 13
  • 50
  • 81
  • 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
  • 28,277
  • 11
  • 59
  • 92
2

You can do it using the debug library:

print(debug.getinfo(f).nparams)
lhf
  • 70,581
  • 9
  • 108
  • 149