lua-users home
lua-l archive

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


Markus Fritsche wrote:
> Luiz Henrique de Figueiredo <lhf <at>> writes:
> > No reason, except that it never came up. What would you use a tonumber
> > metamethod for?
> Well, tonumber accepts only numbers like "42.23", not "1,042.23" or "1.032,42"
> (german number format). Wouldn't be a tonumber-metamethod the right place for
> this? Or is it cleaner to implement this in a function of its own?

I guess this is just not common enough to warrant an extra metamethod.

Metamethods are for implementing general behaviour of user-defined
objects. While most objects have a canonical string representation
(if only for print()'ing them), very few have a canonical numerical
representation (what is tonumber({}) supposed to return?).

Oh and I think you may have something mixed up: Since "1,042.23"
is a string, you'd need a __tonumber metamethod for _all_ strings.
While overriding behaviour for all strings is possible with the
newest Lua 5.1-work6, it's not that useful in this case.

You can easily define your own string -> number conversion function
and (optionally) redefine the tonumber() global to get the same