lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Rici Lake wrote:

On 1-Feb-06, at 2:48 PM, Chris Marrin wrote:


Lua 5.1 has some nice argument checking functions, accessible from C, like luaL_checknumber() and luaL_optstring(). But these are not accessible from Lua, and I have not found anything roughly equivalent. It would be easy to write such things, of course, by doing type(), checking against nil and throwing errors. But is there any "standard" way to do this? I have not been able to find anything...


I usually write:

  a = assert(tonumber(a), "number expected")

or, in the case where there is a sensible default:

  a = assert(tonumber(a or sensible_default), "number expected")

This doesn't produce error messages as nice as luaL_*, though.

Also, it doesn't work with strings, since tostring(x) pretty well always produces a string, in constrast with lua_tostring() which only coerces numbers. It would sometimes be nice to have a Lua function which acted like lua_tostring() and a C function which acted like tostring(). Of course, it's easy enough to write both of those.

Ok, thanks. Here is what I am thinking about adding:

    local _check_foo = checkArgs("int, string='hello', number=3.14")
    function foo(a,b,c)
        _check_foo(arg)
        ...
    end

Where checkArgs() returns a function which does all the checking. This way, I can "compile" the argument checking function for efficiency. This brings up a question. In 5.1, can I use the 'arg' property to pass a table of the args to the checker? Or would I have to go:

    function foo(...)
        _check_foo(...)
        ...
    end

instead? And, if I did either of the above, can I change the visible params to the foo() function? For instance, can I go either:

    if ...[2] == nil then ...[2] = "hello" end

or:

    if arg[2] == nil then arg[2] = "hello" end

and expect param 'b' in foo to now have the value "hello" if it was nil? Or would I need to do something like:

    function foo(a,b,c)
        a,b,c = _check_foo(a,b,c)
        ...
    end

and have the check function do something like:

    local a=...[1], b=...[2], c=...[3]
    if b == nil then b = "hello" end
    return a,b,c

???

--
chris marrin                ,""$,
chris@marrin.com          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'