lua-users home
lua-l archive

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


>> Lua 5 will report this error if the operands don't have the same type.
>> This behavior is different from the + operator.  I don't know the reason
>> behind this.  It does look odd that such constructions (e.g. comparing
>> table-based custom "types" with non-table data) don't seem possible.
>>
>> Does anyone know why it was implemented like this?
>
>It seems to be as you say, yet the pseudo-code for "__lt" in the manual
>doesn't show this! Something is wrong somewhere.

Actually, both the manual and the implementation were wrong. Sorry
about that.

The correct definition for all comparison metamethods is that they
are only applied if (1) both objects have the same type, and (2)
both objects have the same metamethod.

We tried to mimic the behavior of the primitive types. (Of course,
you may consider them broken ;-) 20 + "20" is 40, but 20 == "20" is
false, and 20 < "20" is an error. Similarly, arithmetic metamethods work
for operands of "different types", but equality always reports false
for operands of different types, and comparison raises an error. The
definition of "same type" here is that both operands have the same basic
type and the same metamethod for that operator (so there is no ambiguity
about which metamethod to call).

-- Roberto