ceylon/ceylon-spec — Issue #84: Ability to daisy-chain comparisons
Author: FroMage
Created: 2011-12-02 10:14:09+00:00
State: open

The #1 biggest let down with comparison operators like `<=>` or `Comparable.compareTo()` in Java is that you can't easily daisy-chain them:

```java
int compareTo(Other other){
int result = firstName.compareTo(other.firstName);
if(result != 0)
return result;
result = lastName.compareTo(other.lastName);
if(result != 0)
return result;
result = age.compareTo(other.age);
if(result != 0)
return result;
...
}
```

When in Perl you'd write:

```Perl
sub compareTo(){
my ($this, $other) = @_;
return $this->firstName <=> $other.firstName
|| $this->lastName <=> $other.lastName
|| $this->age <=> $other.age;
}
```

Because boolean operators like `||` return _truthy_ values (`-1` and `1` are _truthy_ while `0` is `falsy`), and `<=>` returns a number;

We should find a way to support this somehow, in a style compatible with Ceylon, because writing comparators is a very frequent task.