lua-users home
lua-l archive

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


On 25 December 2014 at 01:41, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2014-12-25 1:05 GMT+02:00 Daurnimator <quae@daurnimator.com>:
>
>> The new respect for the __name metafield by argument checking is
>> actually a bigger change I didn't even know about until you mentioned
>> it on IRC today.
>
> At this stage it is an implementation detail: all that the manual says
> about it is in the description of API function `luaL_newmetatable`,
> not where it will be used.
>

As it happens, we're on the cusp of releasing a new implementation;
Infact, it will probably be the reference implementation!
This would be a nice thing to get correct.

On 25 December 2014 at 07:58, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> > math.sin(setmetatable({},{__name = 'number'}))
>> sandbox:1: bad argument #1 to 'sin' (number expected, got number)
>
> So, don't use the name of a basic type as the name of your type...
>
>> Could this be changed to something like:
>> "bad argument #%d to '%s' (%s (%s) expected, got %s (%s))"
>> where each first %s is the base type (as returned by lua_typename),
>> and the %s in parenthesis is the __name metafield?
>
> And then get this?
>         bad argument #1 to 'sin' (number (number) expected, got number (number))
>

I see two easy options:
1. don't have the parenthesised __name if the metafield does not exist
2. for numbers, we can have "float" vs "integer"

This would be a much more informative error message:

    bad argument #1 to 'sin' (number (float) expected, got userdata (number))