lua-users home
lua-l archive

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


On 22/10/2019 15.25, Roberto Ierusalimschy wrote:
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.
In general, other metamethods do not need (and do not have) this
behavior. (At least mine don't.)
OK, agree.

Just for completeness:  It *is* necessary to manually retry the right
side's metamethod when working with multiple levels of partially
compatible types a < b < c, e.g. the string situation of number < string
< (whatever a user may define), or a different (maybe simpler?) example:
numbers < complex numbers < vectors. If vectors treat non-vectors as
constants, number-anything works as usual or Lua uses the right side's
metamethod, vector-complex / vector-number work as expected, but
complex-vector needs to manually fall back to the right side (just like
Lua does for strings).

-- nobody