lua-users home
lua-l archive

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


2009/4/27 Mike Crowe <drmikecrowe@gmail.com>:
> Hi folks,
>
> I was briefly caught by doing this test:
>
>   if tonumber(something) > 0 then
>      ...
>   end
>
> with something=nil.
> I'm not advocating a change, but what's the thought-process behind this
> behavior?  Wouldn't you consider the numeric value of nil to be 0?

It seems to me that tonumber() aims to operate on things which are
already representations of numbers (numbers and strings in this case),
and returns nil when its argument isn't a representation of a number.
nil represents nothing, and while you could argue that 0 represents
the number of things in nothing, it doesn't represent nothing in the
context of this environment.