0

For example, can I declare a metamethod for __index which takes two arguments, then do something like myuserdata[somearg1, somearg2]? Not that I want to use it or that I have any idea of why it would be useful, I'm just wondering if in my library that deals with binding c++ to Lua I can and should enforce a right (except for __call metamethod of course) number of arguments of the function bound as metamethod.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65

3 Answers3

1

Lua doesn't enforce parameter counts on functions in general: http://ideone.com/kAynR

That said, the metamethods which map to syntax are bound by the syntax - for instance, Lua syntax doesn't allow ,-separated values inside [] indexing:

luac: ']' expected near ','
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 1
    That'd be a mighty handy feature, though; I don't see any reason not to propose that for the next major release of Lua. How much complexity & size would it add to the lexer & bytecode? – Deco May 02 '12 at 02:29
  • Probably more than it's worth. There's always the option to pass a string instead and have the handling code deal with it (e.g. `foo["1,2"]`). – Amber May 02 '12 at 02:34
1

Lua functions can receive an arbitrary number of functions. This is true of your metamethods as well.

However, metatables don't let you change the arity of operators. A metamethod for unary operators (not, #, etc.) is only going to be passed on one argument. A metamethod for binary operators (+, %, ==, ^, .., etc.) is only going to be passed two arguments.

Table indexing operations are defined in the Lua grammar as taking one argument, so that's all you can use and all your metamethod for indexing will receive.

The only metamethod that may receive an arbitrary number of aruments is call.

Mud
  • 28,277
  • 11
  • 59
  • 92
0

No, the index operator can only take one argument, as in C++. While the function for the index operator can have as many arguments as you want, if you actually try to use more than one inside the [] you'll get an error.

It depends on the operator. So you neither can nor need to "enforce" that.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • I don't need, but it would be nice to help the user of my library to avoid errors like passing a two arguments function as the unary - operation metamethod. – Lorenzo Pistone May 01 '12 at 22:28
  • @LorenzoPistone you can't pass two arguments to unary minus (unless you use the function name from the metatable), so you don't need to. – Seth Carnegie May 01 '12 at 22:29
  • @SethCarneige There must be a misunderstanding. As Amber pointed out, "Lua doesn't enforce parameter counts on functions in general".Since my library takes any C++ function and generates at compile time a `lua_CFunction` which,fetching the appropriate argument from the stack, works as a proxy the original function,and will be set as the appropriate field in the metatable of my userdata, the user might pass to this machinery a function with a "wrong" number of arguments, and so making the proxy gather more arguments than the number it can ever gather from a Lua expression like `[]` (which is 1) – Lorenzo Pistone May 01 '12 at 22:38
  • @LorenzoPistone if your C++ function generates _at compile time_ another function, then you don't need to worry because your function will be checked statically for the # of args it receives. I can't really know what you mean unless I see your code or something, but since apparently your problem is solved, that's OK. – Seth Carnegie May 01 '12 at 22:49