lua-users home
lua-l archive

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


On 23/10/2012, at 11:04 PM, David Collier <myshkin@cix.co.uk> wrote:

>>> I suppose I could write a function "checkargs"
>> 
>> iirc the Sierra Wireless folks did something like that.
> 
> Indeed we have...

If you're talking about run-time rather than static type-checking, then [1] shamelessly copies the sierra wireless solution, except that it runs as a debug hook, reads documentation comments rather than requiring a check function and is in Lua as opposed to C (nothing against C, just that a Lua-only solution doesn't require compilation).


    --- prints the string s n times
    -- @tparam number n how many times to print the string
    -- @tparam string s the string to print
    function printn(s, n)
      for i = 1, n do print(s) end
    end

    printn(10, "hello")


    $ lua -largcheck test/ldoc.lua 
    lua: test/ldoc.lua:8: bad argument #1 to 'printn' (string expected, got number '10')


It's not as polished as the Sierra solution yet, and currently quite slow (as hooks in Lua tend to be), but I like that the comments double as documentation comments.

cheers,
Geoff


[1] https://github.com/geoffleyland/argcheck