Why was the spaceship operator <=>
chosen to have one equal sign rather than two? Is this seen as inconsistent with one equal sign usually meaning assignment, and two meaning comparison?
The <=> operator is often called the “spaceship operator” because it looks like Darth Vader's ship in Star Wars. Python doesn't have a spaceship operator, but you can get the same effect with numpy. sign(a-b) . For example, suppose you wanted to write a program to compare two integers.
The three-way comparison operator “<=>” is called a spaceship operator. The spaceship operator determines for two objects A and B whether A < B, A = B, or A > B. The spaceship operator or the compiler can auto-generate it for us.
The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b . Comparisons are performed according to PHP's usual type comparison rules.
The C++20 three-way comparison operator <=> (commonly nicknamed the spaceship operator due to its appearance) compares two items and describes the result. It's called the three-way comparison because there are five possible results: less, equal, equivalent, greater, and unordered.
Why would it have two? There's only one in <=
, >=
and !=
. It's not inconsistent at all. Only ==
is inconsistent, and that's to avoid conflicts with the assignment operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With