Comparing
The six comparisons, and the answer to the question left hanging about the = sign.
Comparing two values gives a bool — true or false. You have already met one without paying
it much attention: the registered variable in the “Showing a value” sub-section.
@entry(start)
object MyProgram {
start() {
var age := 42;
Console.print(text: $"is age 42? {age = 42}");
Console.print(text: $"does age differ from 42? {age != 42}");
Console.print(text: $"is age over 18? {age > 18}");
Console.print(text: $"has age reached 18? {age >= 18}");
}
}
Four questions, four answers.
= compares, and never assigns
Here is the answer promised in 2.1. The = sign is reserved for equality: age = 42 does
not put 42 into age, it asks whether age is 42, and gives back true or false.
That is why assigning is written :=. Each gesture has its own sign, and the equals sign means
what it has meant in mathematics all along.
So there is no == in this language. Where C, Java or Python write = to assign and == to
compare, Archon has no need to double the sign to tell the two apart — they do not look alike in
the first place.
The gain is not cosmetic. Confusing = and == is one of the oldest and costliest mistakes of
the C family: if (x = 5) is perfectly valid code there, assigning where it meant to compare,
and going through without a word. Here the mistake has no shape: you cannot write one believing
you wrote the other.
The six
| written | true when |
|---|---|
a = b | both hold the same thing |
a != b | they differ |
a < b | a is strictly smaller |
a > b | a is strictly greater |
a <= b | a is smaller, or equal |
a >= b | a is greater, or equal |
!= reads “differs from”. It is the only place in the language where the exclamation mark
appears: it is reserved for this use, and negation itself is written out in full — we come to it
in 3.3.
Texts compare too
= is not only for numbers. Two texts are compared by their contents:
var word := "Alexandria";
Console.print(text: $"{word = "Alexandria"}"); // true
Two identical texts are equal, even if they were written at two different places in the program.
Your turn
- Change
42to another value in the four questions, and predict each answer before running. - Compare two texts that differ only by a capital —
"simon" = "Simon". The comparison is exact, it forgives nothing. - Write
age = 43;alone on a line, as a statement. The compiler refuses — a comparison whose result is thrown away does nothing — and names in passing the sign you may have meant.