[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: metamethod __lt oddity?
- From: MichaelL@...
- Date: Mon, 20 Jan 2003 10:18:11 -0500
FYI, Ruby defines a "comparison" operator (<=>) which returns -1, 0, or 1.
It then derives all other comparisons from that.
<email@example.com To: Multiple recipients of list <firstname.lastname@example.org>
Sent by: Subject: Re: metamethod __lt oddity?
01/18/2003 04:52 AM
Please respond to
>> It seems to be as you say, yet the pseudo-code for "__lt" in the manual
>> doesn't show this! Something is wrong somewhere.
> The code clearly requires the objects to be of the same type for the
> __le, and __eq metamethods. (see lvm.c around line 210). So I suppose the
> documentation is wrong -- I would guess that the documentation has not
> been updated from Lua 4.
> The addition of the __eq method creates some problems; for one thing, it
> could make equality tests quite slow, and equality is tested quite a lot.
True, equality is indeed tested quite a lot, but how often on tables and
userdata (which are now the only types that can have metamethods) as
The slowing down would only occur when objects that possessed metatables
> Otherwise I will return to my original suggestion that there be two
> different equality operators, one of which is object equality and the
> other of which can be overridden (eg. == and ===).
Useful, but I don't think we need an extra symbol. We could have "raweq" to
test for raw equality, just like we have "rawget/rawset" for the indexing
symbol "[ ]".
On a slight tangent, the comparison metamethods don't include the full set
lt, le, gt, ge.
Instead they assume that these operators obey ordering properties, and so
(a < b) == (b > a) -- so 'gt' can be discarded.
(a <= b) == (b >= a) -- so 'ge' can be discarded.
However, they then go on to acknowledge the further property that:
(a <= b) == not (a > b) == not (b < a)
but they don't discard 'le' (or 'lt') leaving us with only one comparison
metamethod. They keep two, which could then be made contradictory. Why? If
the authors didn't want to supply the whole set (lt, le, gt, ge) why didn't
they just reduce it to a single operator?