[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Just curious: Why is tonumber(nil) not 0?
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Mon, 27 Apr 2009 17:19:19 +0200
2009/4/27 Evan DeMond <evan.demond@gmail.com>:
> Just to look at things another way, then, is it troubling that tonumber()
> isn't guaranteed to return a number?
> Not saying that I think that nil should be convertible to a numeric value,
> but I'd like to hear others' thoughts on that point. =)
tonumber is not taking an arbitrary value and converting it to a
numerical value. It is taking an already numerical value, represented
by a Lua object which type *is not* 'number', and returns the same
numerical value in a Lua object which type *is* 'number'. See it as a
type cast. Just like a C type cast you can lose precision too.
For me it seems perfectly natural that if you give it a value which is
not numerical it is considered an error. It could have returned nil
plus an error message, but I guess there was no real need to be more
specific on the error reason so only nil is returned.