Carbon provides equality and relational comparison operators, each with a standard mathematical meaning:
Category | Operator | Example | Mathematical meaning | Description |
---|---|---|---|---|
Equality | == |
x == y |
= | Equality or equal to |
Equality | != |
x != y |
≠ | Inequality or not equal to |
Relational | < |
x < y |
< | Less than |
Relational | <= |
x <= y |
≤ | Less than or equal to |
Relational | > |
x > y |
> | Less than |
Relational | >= |
x >= y |
≥ | Less than or equal to |
Comparison operators all return a bool
; they evaluate to true
when the
indicated comparison is true. All comparison operators are infix binary
operators.
These operators have predefined meanings for some of Carbon's built-in types, as well as for simple "data" types like structs and tuples.
User-defined types can define the meaning of these operations by implementing an interface provided as part of the Carbon standard library.
The comparison operators are all at the same precedence level. This level is
lower than operators used to compute (non-bool
) values, higher than the
logical operators and
and or
, and incomparable with
the precedence of not
.
For example:
// ✅ Valid: precedence provides order of evaluation.
if (n + m * 3 < n * n and 3 < m and m < 6) {
...
}
// The above is equivalent to:
if (((n + (m * 3)) < (n * n)) and ((3 < m) and (m < 6))) {
...
}
// ❌ Invalid due to ambiguity: `(not a) == b` or `not (a == b)`?
if (not a == b) {
...
}
// ❌ Invalid due to precedence: write `a == (not b)`.
if (a == not b) {
...
}
// ❌ Invalid due to precedence: write `not (f < 5.0)`.
if (not f < 5.0) {
....
}
The comparison operators are non-associative. For example:
// ❌ Invalid: write `3 < m and m < 6`.
if (3 < m < 6) {
...
}
// ❌ Invalid: write `a == b and b == c`.
if (a == b == c) {
...
}
// ❌ Invalid: write `(m > 1) == (n > 1)`.
if (m > 1 == n > 1) {
...
}
Built-in comparisons are permitted in three cases:
- When both operands are of standard Carbon integer types (
Int(n)
orUnsigned(n)
). - When both operands are of standard Carbon floating-point types (
Float(n)
). - When one operand is of floating-point type and the other is of integer type, if all values of the integer type can be exactly represented in the floating-point type.
In each case, the result is the mathematically-correct answer. This applies even
when comparing Int(n)
with Unsigned(m)
.
For example:
// ✅ Valid: Fits case #1. The value of `compared` is `true` because `a` is less
// than `b`, even though the result of a wrapping `i32` or `u32` comparison
// would be `false`.
fn Compare(a: i32, b: u32) -> bool { return a < b; }
let compared: bool = Compare(-1, 4_000_000_000);
// ❌ Invalid: Doesn't fit case #3 because `i64` values in general are not
// exactly representable in the type `f32`.
let float: f32 = 1.0e18;
let integer: i64 = 1_000_000_000_000_000_000;
let eq: bool = float == integer;
Comparisons involving integer and floating-point constants are not covered by these rules and are discussed separately.
We support the following implicit conversions:
- From
Int(n)
toInt(m)
ifm > n
. - From
Unsigned(n)
toInt(m)
orUnsigned(m)
ifm > n
. - From
Float(n)
toFloat(m)
ifm > n
. - From
Int(n)
toFloat(m)
ifFloat(m)
can represent all values ofInt(n)
.
These rules can be summarized as: a type T
can be converted to U
if every
value of type T
is a value of type U
.
Implicit conversions are also supported from certain kinds of integer and
floating-point constants to Int(n)
and Float(n)
types, if the constant can
be represented in the type.
All built-in comparisons can be viewed as performing implicit conversions on at
most one of the operands in order to reach a suitable pair of identical or very
similar types, and then performing a comparison on those types. The target types
for these implicit conversions are, for each suitable value n
:
Int(n)
versusInt(n)
Unsigned(n)
versusUnsigned(n)
Int(n)
versusUnsigned(n)
Unsigned(n)
versusInt(n)
Float(n)
versusFloat(n)
There will in general be multiple combinations of implicit conversions that will
lead to one of the above forms, but we will arrive at the same result regardless
of which is selected, because all comparisons are mathematically correct and all
implicit conversions are lossless. Implementations are expected to do whatever
is most efficient: for example, for u16 < i32
it is likely that the best
choice would be to promote the u16
to i32
, not u32
.
Because we only ever convert at most one operand, we never use an intermediate
type that is larger than both input types. For example, both i32
and f32
can
be implicitly converted to f64
, but we do not permit comparisons between i32
and f32
even though we could perform those comparisons in f64
. If such
comparisons were permitted, the results could be surprising:
// `i32` can exactly represent this value.
var integer: i32 = 2_000_000_001;
// This value is within the representable range for `f32`, but will be rounded
// to 2_000_000_000.0 due to the limited precision of `f32`.
var float: f32 = 2_000_000_001.0;
// ❌ Invalid: `f32` cannot exactly represent all values of `i32`.
if (integer == float) {
...
}
// ✅ Valid: An explicit cast to `f64` on either side makes the code valid, but
// will compare unequal because `float` was rounded to 2_000_000_000.0
// but `integer` will convert to exactly 2_000_000_001.0.
if (integer == float as f64) {
...
}
if (integer as f64 == float) {
...
}
The two kinds of mixed-type comparison may be less efficient than the other kinds due to the slightly wider domain.
Note that this approach diverges from C++, which would convert both operands to a common type first, sometimes performing a lossy conversion potentially giving an incorrect result, sometimes converting both operands, and sometimes using a wider type than either of the operand types.
We permit the following comparisons involving constants:
- A constant can be compared with a value of any type to which it can be implicitly converted.
- Any two constants can be compared, even if there is no type that can represent both.
As described in implicit conversions, integer constants can be implicitly converted to any integer or floating-point type that can represent their value, and floating-point constants can be implicitly converted to any floating-point type that can represent their value.
Note that this disallows comparisons between, for example, i32
and an integer
literal that cannot be represented in i32
. Such comparisons would always be
tautological. This decision should be revisited if it proves problematic in
practice, for example in templated code where the literal is sometimes in range.
User-defined types can extend the behavior of the comparison operators by implementing interfaces. In this section, various properties are specified that such implementations "should" satisfy. These properties are not enforced in general, but the standard library might detect violations of some of them in some circumstances. These properties may be assumed by generic code, resulting in unexpected behavior if they are violated.
Comparison operators can be provided for user-defined types by implementing the
EqWith
and OrderedWith
interfaces.
The EqWith
interface is used to define the semantics of the ==
and !=
operators for a given pair of types:
interface EqWith(U:! Type) {
fn Equal[me: Self](u: U) -> bool;
default fn NotEqual[me: Self](u: U) -> bool {
return not (me == u);
}
}
constraint Eq {
extends EqWith(Self);
}
Given x: T
and y: U
:
- The expression
x == y
callsx.(EqWith(U).Equal)(y)
. - The expression
x != y
callsx.(EqWith(U).NotEqual)(y)
.
class Path {
private var drive: String;
private var path: String;
private fn CanonicalPath[me: Self]() -> String;
external impl as Eq {
fn Equal[me: Self](other: Self) -> bool {
return (me.drive, me.CanonicalPath()) ==
(other.drive, other.CanonicalPath());
}
}
}
The EqWith
overload is selected without considering possible implicit
conversions. To permit implicit conversions in the operands of an ==
overload,
the
like
operator
can be used:
class MyInt {
var value: i32;
fn Value[me: Self]() -> i32 { return me.value; }
}
external impl i32 as ImplicitAs(MyInt);
external impl like MyInt as EqWith(like MyInt) {
fn Equal[me: Self](other: Self) -> bool {
return me.Value() == other.Value();
}
}
fn CompareBothWays(a: MyInt, b: i32, c: MyInt) -> bool {
// OK, calls above implementation three times.
return a == a and a != b and b == c;
}
The behavior of NotEqual
can be overridden separately from the behavior of
Equal
to support cases like floating-point NaN values, where two values can
compare neither equal nor not-equal, and thus both functions would return
false
. However, an implementation of EqWith
should not allow both Equal
and NotEqual
to return true
for the same pair of values. Additionally, these
operations should have no observable side-effects.
external impl like MyFloat as EqWith(like MyFloat) {
fn Equal[me: MyFloat](other: MyFloat) -> bool {
if (me.IsNaN() or other.IsNaN()) {
return false;
}
return me.Representation() == other.Representation();
}
fn NotEqual[me: MyFloat](other: MyFloat) -> bool {
if (me.IsNaN() or other.IsNaN()) {
return false;
}
return me.Representation() != other.Representation();
}
}
Heterogeneous comparisons must be defined both ways around:
external impl like MyInt as EqWith(like MyFloat);
external impl like MyFloat as EqWith(like MyInt);
TODO: Add an adapter to the standard library to make it easy to define the reverse comparison.
The OrderedWith
interface is used to define the semantics of the <
, <=
,
>
, and >=
operators for a given pair of types.
choice Ordering {
Less,
Equivalent,
Greater,
Incomparable
}
interface OrderedWith(U:! Type) {
fn Compare[me: Self](u: U) -> Ordering;
default fn Less[me: Self](u: U) -> bool {
return me.Compare(u) == Ordering.Less;
}
default fn LessOrEquivalent[me: Self](u: U) -> bool {
let c: Ordering = me.Compare(u);
return c == Ordering.Less or c == Ordering.Equivalent;
}
default fn Greater[me: Self](u: U) -> bool {
return me.Compare(u) == Ordering.Greater;
}
default fn GreaterOrEquivalent[me: Self](u: U) -> bool {
let c: Ordering = me.Compare(u);
return c == Ordering.Greater or c == Ordering.Equivalent;
}
}
constraint Ordered {
extends OrderedWith(Self);
}
// Ordering.Less < Ordering.Equivalent < Ordering.Greater.
// Ordering.Incomparable is incomparable with all three.
external impl Ordering as Ordered;
TODO: Revise the above when we have a concrete design for enumerated types.
Given x: T
and y: U
:
- The expression
x < y
callsx.(OrderedWith(U).Less)(y)
. - The expression
x <= y
callsx.(OrderedWith(U).LessOrEquivalent)(y)
. - The expression
x > y
callsx.(OrderedWith(U).Greater)(y)
. - The expression
x >= y
callsx.(OrderedWith(U).GreaterOrEquivalent)(y)
.
For example:
class MyWidget {
var width: i32;
var height: i32;
fn Size[me: Self]() -> i32 { return me.width * me.height; }
// Widgets are normally ordered by size.
external impl as Ordered {
fn Compare[me: Self](other: Self) -> Ordering {
return me.Size().(Ordered.Compare)(other.Size());
}
}
}
fn F(a: MyWidget, b: MyWidget) -> bool {
return a <= b;
}
As for EqWith
, the
like
operator
can be used to permit implicit conversions when invoking a comparison, and
heterogeneous comparisons must be defined both ways around:
fn ReverseOrdering(o: Ordering) -> Ordering {
return Ordering.Equivalent.(Ordered.Compare)(o);
}
external impl like MyInt as OrderedWith(like MyFloat);
external impl like MyFloat as OrderedWith(like MyInt) {
fn Compare[me: Self](other: Self) -> Ordering {
return Reverse(other.(OrderedWith(Self).Compare)(me));
}
}
The default implementations of Less
, LessOrEquivalent
, Greater
, and
GreaterOrEquivalent
can be overridden if a more efficient version can be
implemented. The behaviors of such overrides should follow those of the above
default implementations, and the members of an OrderedWith
implementation
should have no observable side-effects.
OrderedWith
implementations should be transitive. That is, given V:! Type
,
U:! OrderedWith(V)
, T:! OrderedWith(U) & OrderedWith(V)
, a: T
, b: U
,
c: V
, then:
- If
a <= b
andb <= c
thena <= c
, and moreover if eithera < b
orb < c
thena < c
. - If
a >= b
andb >= c
thena >= c
, and moreover if eithera > b
orb > c
thena > c
. - If
a
andb
are equivalent, thena.Compare(c) == b.Compare(c)
. Similarly, ifb
andc
are equivalent, thena.Compare(b) == a.Compare(c)
.
OrderedWith
implementations should also be consistent under reversal. That
is, given types T
and U
where T is OrderedWith(U)
and
U is OrderedWith(T)
, and values a: T
and b: U
:
- If
a.(OrderedWith.Compare)(b)
isOrdering.Greater
, thenb.(OrderedWith.Compare)(a)
isOrdering.Less
, and the other way around. - Otherwise,
a.(OrderedWith.Compare)(b)
returns the same value asb.(OrderedWith.Compare)(a)
.
There is no expectation that an Ordered
implementation be a total order, a
weak order, or a partial order, and in particular the implementation for
floating-point types is none of these because NaN values do not compare less
than or equivalent to themselves.
TODO: The standard library should provide a way to specify that an ordering is a weak, partial, or total ordering, and a way to request such an ordering in a generic.
There is no requirement that a pair of types that implements OrderedWith
also
implements EqWith
. If a pair of types does implement both, however, the
equality relation provided by x.(EqWith.Equal)(y)
should be a refinement of
the equivalence relation provided by
x.(OrderedWith.Compare)(y) == Ordering.Equivalent
.
TODO: Support a lower-level extensibility mechanism that allows a result
type other than bool
.
In addition to being defined for standard Carbon numeric types, equality and relational comparisons are also defined for all "data" types:
Relational comparisons for these types provide a lexicographical ordering. In each case, the comparison is only available if it is supported by all element types.
Because implicit conversions between data classes can reorder fields, the implementations for data classes do not permit implicit conversions on their arguments in general. Instead:
- Equality comparisons are permitted between any two data classes that have
the same unordered set of field names, if each corresponding pair of
fields has an
EqWith
implementation. Fields are compared in the order they appear in the left-hand operand. - Relational comparisons are permitted between any two data classes that have
the same ordered sequence of field names, if each corresponding pair of
fields has an
OrderedWith
implementation. Fields are compared in order.
Comparisons between tuples permit implicit conversions for either operand, but not both.
The bool
type should be treated as a choice type, and so should support
equality comparisons and relational comparisons if and only if choice types do
in general. That decision is left to a future proposal.
- Alternative symbols
- Chained comparisons
- Convert operands like C++
- Provide a three-way comparison operator
- Allow comparisons as the operand of
not
- Rename
OrderedWith
toComparableWith
- Proposal #702: Comparison operators
- Proposal #1178: Rework operator interfaces
- Issue #710: Default comparison for data classes