lua-users home
lua-l archive

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


> >The string library uses metamethods that try to coerce strings to numbers
> >in all arithmetic operations. If the conversion fails, the library calls
> >the metamethod of the other operand (if present) or it raises an error.
> Actually it only tries the metamethod of the right side, to prevent a
> "ping-pong" loop with some other metamethod that applies the same logic.

That explanation is not quite right. It tries only the right side
because Lua already tried the left side. If the left operand had a
metamethod, Lua would have called it instead of this [right operand]
one that is running.


> It might be useful to be explicit about this so that other programmers
> reading the manual understand the potential problem and also write their
> metamethods in such a loop-preventing way.

In general, other metamethods do not need (and do not have) this
behavior. (At least mine don't.) Strings only act this way to be
compatible with previous versions, where they were builtin and
therefore did not follow a "metamethod logic".

-- Roberto