Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the spaceship operator have only one equal sign in it?

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?

like image 487
Andrew Grimm Avatar asked Apr 01 '11 01:04

Andrew Grimm


People also ask

What is a symbol for spaceship operator?

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.

How to define spaceship operator c++?

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.

What is the use of spaceship operator?

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.

What is three-way comparison operator?

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.


1 Answers

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.

like image 63
ikegami Avatar answered Sep 22 '22 18:09

ikegami