Files
carbon-lang/core/prelude/operators/comparison.carbon
T
Richard Smith b8814f6c80 Add named constraints for Eq and Ordered. (#7714)
Also add a default for `EqWith.NotEqual`.

Switch advent examples to use these named constraints, and also go
through all the other TODOs in the advent examples and fix the ones that
are trivially fixable now.
2026-09-03 16:20:13 +00:00

61 lines
1.9 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
package Core library "prelude/operators/comparison";
export import library "prelude/types/bool";
import library "prelude/copy";
import library "prelude/types/int_literal";
// Equality comparison: `a == b` and `a != b`.
interface EqWith(Other: type) {
fn Equal(self, other: Other) -> bool;
default fn NotEqual(self, other: Other) -> bool {
return not self.Equal(other);
}
}
constraint Eq {
extend require impls EqWith(Self);
}
// Relational comparison: `a < b`, `a <= b`, `a > b`, `a >= b`.
interface OrderedWith(Other: type) {
// TODO: fn Compare
// TODO: Each of the below should be a `default fn` implemented in terms of
// Compare.
fn Less(self, other: Other) -> bool;
fn LessOrEquivalent(self, other: Other) -> bool;
fn Greater(self, other: Other) -> bool;
fn GreaterOrEquivalent(self, other: Other) -> bool;
}
constraint Ordered {
extend require impls OrderedWith(Self);
}
// Equality comparison for `bool`.
// Note that this must be provided in this library as `bool` doesn't have any
// associated libraries of its own.
impl bool as EqWith(Self) {
fn Equal(self, other: Self) -> bool = "bool.eq";
fn NotEqual(self, other: Self) -> bool = "bool.neq";
}
// Operations for IntLiteral. These need to be here because IntLiteral has no
// associated library of its own.
impl IntLiteral as EqWith(Self) {
fn Equal(self, other: Self) -> bool = "int.eq";
fn NotEqual(self, other: Self) -> bool = "int.neq";
}
impl IntLiteral as OrderedWith(Self) {
// TODO: fn Compare
fn Less(self, other: Self) -> bool = "int.less";
fn LessOrEquivalent(self, other: Self) -> bool = "int.less_eq";
fn Greater(self, other: Self) -> bool = "int.greater";
fn GreaterOrEquivalent(self, other: Self) -> bool = "int.greater_eq";
}