Project

General

Profile

Actions

Feature #6555

closed

New comparison operators

Added by jEFF (Jean-François Berroyer) almost 12 years ago. Updated over 11 years ago.

Status:
Rejected
Target version:
[ruby-core:45471]

Description

=begin
x <=> y returns -1, 0, 1 or nil
(x <=> y) == -1 means "x less than y" and is the same as x < y
(x <=> y) == 0 means "x equal y" is the same as x == y
(x <=> y) == 1 means "x greater than y" and is the same as x > y
(x <=> y) == nil means "x not comparable with y" and is the same as !(x <=> y)
We see there is no short syntax to test if two objects are not comparable.
Can we have something like (({x >< y})), provided by (({Comparable})) module, that would mean "x and y are not comparable" ?

As (({x != y})) is a short syntax for (({!(x == y)})), can we have (({x !< y})) for (({!(x < y)})) and (({x !<= y})) for (({!(x <= y)})) and so on ?

As (({x <= y})) is the same as (({(x < y or x == y)})), can we have (({x <> y})) for (({(x < y or x > y)})) ?

Here is a list of all possible comparison operators there could be (and their meanings) :

< return true when <=> return -1 ("less than")
== return true when <=> return 0 ("egal")

return true when <=> return 1 ("greater than")
<= return true when <=> return -1 or 0 ("equal or less than")
= return true when <=> return 1 or 0 ("equal or greater than")
<> return true when <=> return 1 or -1 ("less or greater than" that is to say "comparable and different")
< return true when <=> return nil ("not comparable")
!< return true when <=> return 0 or 1 or nil ("not less than", that is different from "greater than or eqal" that involves "comparable")
!= return true when <=> return -1 or 1 or nil ("not equal", that is different from "less or greater than" that involves "comparable")
!> return true when <=> return -1 or 0 or nil ("not greater than", that is different from "less than or eqal" that involves "comparable")
!<= return true when <=> return nil or 1 ("not equal nor less than", that is different from "greater than" that involves "comparable")
!>= return true when <=> return nil or -1 ("not equal nor greater than", that is different from "less than" that involves "comparable")
!<> return true when <=> return 0 or nil ("not less nor greater than", that is different from "egal" that involves "comparable")
!>< return true when <=> return -1 or 0 or 1 ("not not comparable" or just "comparable", the same as <=> but returning a boolean)

All these operators would be very useful, especially when working with partial orders (like subset-inclusion for exemple).

Perhaps (({x !== y})) should also be a short syntax for (({!(x === y)})).
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0