基本用法

太空船運算子在左運算子較小時返回 -1,在運算子相等時返回 0,否則返回 1

assert 10 <=> 20 == -1
assert 10 <=> 10 ​== 0
assert 30 <=> 10 == 1

assert 'a' <=> 'b' == -1
assert 'a' <=> 'a'​== 0
assert 'b' <=> 'a' == 1​​

它等同於 Comparable.compareTo 方法:

assert 10.compareTo(20) == (10 <=> 20)
assert 'a'.compareTo('b') == ('a' <=> 'b')