torch / argcheck

A powerful (and blazing fast) argument checker and function overloading system for Lua or LuaJIT
Other
54 stars 17 forks source link

Problem parsing more than 9 input arguments #3

Open fmassa opened 9 years ago

fmassa commented 9 years ago

The following code snippet reproduces the problem:

argcheck = require 'argcheck'
initcheck = argcheck{
  {name="x1",type="string",help="x1",default="a"},
  {name="x2",type="string",help="x2",default="a"},
  {name="x3",type="string",help="x3",default="a"},
  {name="x4",type="string",help="x4",default="a"},
  {name="x5",type="string",help="x5",default="a"},
  {name="x6",type="string",help="x6",default="a"},
  {name="x7",type="string",help="x7",default="a"},
  {name="x8",type="string",help="x8",default="a"},
  {name="x9",type="string",help="x9",default="a"},
  {name="x10",type="string",help="x10",default="a"}
}

Here is the error I get:

/usr/local/share/lua/5.1/argcheck/init.lua:105: could not generate argument checker: [string "argcheck"]:35093: control structure too long near 'end'
stack traceback:
    [C]: in function 'error'
    /usr/local/share/lua/5.1/argcheck/init.lua:105: in function 'argcheck'
    [string "initcheck = argcheck{..."]:1: in main chunk
    [C]: in function 'xpcall'
    /usr/local/share/lua/5.1/trepl/init.lua:567: in function </usr/local/share/lua/5.1/trepl/init.lua:468>
andresy commented 9 years ago

You have many default variables. The case of ordered arguments: initcheck("a", "b", "c"...) generates only 10 cases, that is ok.

But in the named argument case, that generates a lot of things, because you can specify any argument to be there or not (and argcheck checks all possible cases) initcheck{x3="a", x5="b", x10="e") -- note this is not possible with ordered arguments. This case generate 15K lines of code (see with argcheck{debug=true, ...}). Unfortunately, luajit (which has much more limitations than lua) does not support it (lua is ok though).

If you do not need named arguments, then deactivate it with argcheck{nonamed=true, ...}, and you will be fine. If you really need named arguments for that case... hmmm... i have to think about workarounds which would work with luajit. I could also add an option such that the behavior is the same than with ordered arguments (like initcheck{x1="a", x2="b} is ok, but initcheck{x2="a", x3="b"} is not).

bamos commented 8 years ago

Hi @andresy - any further thoughts on how to solve this? I'd like to overcome this in my application without using hacks like combining arguments into tables, manually defaulting values, or not using named arguments. I'd be happy to contribute some code to resolve this.

-Brandon.

fmassa commented 8 years ago

@bamos I wrote a simple workaround function sometime ago specifically for this case. It would sure be much better to solve this problem in argcheck though.