lua-users home
lua-l archive

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


On Sun, May 9, 2010 at 8:57 PM, Andre Leiradella
<aleirade@sct.microlink.com.br> wrote:
> On 09/05/2010 22:13, Henk Boom wrote:
>
> On 9 May 2010 20:57, HyperHacker <hyperhacker@gmail.com> wrote:
>>
>> > Use metatables. Accessing a key from any type which doesn't have a
>> > metatable
>> > for it besides a table is an error.
>> >
>> > Cheers,
>> >
>> > Andre
>> >
>>
>> That's what I've been doing; the question is should nil have a
>> metatable to make __index return nil (and perhaps print a warning)?
>
> Printing a warning would make it pretty much unusable as a normal feature
> though, so I don't see the point. It seems to me you'd want it on all the
> way or not at all.
>     henk
>
> I've never needed to set a metatable for nil, and the manual doesn't make it
> clear (at least for me) whether it's possible or not to do that. If it isn't
> I think it may be a nice feature to have.
>
> Having the possibility to set a metatable for nil, the warning could be
> emitted by the metamethods; it doesn't need to be emitted by the VM. Not all
> environments where Lua runs have a console or some other mean to output
> messages to the user.
>
> Cheers,
>
> Andre
>

It's quite possible, thought you need to use debug.setmetatable rather
than the core setmetatable function. I did a silly proof of concept a
while ago by adding an __index metamethod to nil, booleans, and
numbers, allowing access to an is_nil method. It worked very well,
which was nice to see.

~Jonathan Castello