lua-users home
lua-l archive

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


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.