Files
sakurac/core/traits/equatable.sakura
T

37 lines
1.5 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file equatable.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the equatable trait, which a type implements to be compared with
#! `==`, and `equals`, which compares two values through it.
module core;
#! A type whose values can be compared for equality, by a method comparing a
#! value with another of the same type.
#!
#! A type that is `equatable` is compared by `==` and `!=`, which call its
#! `equals`, and the method can be called by name too.
pub trait equatable {
#! Compares this value with another of the same type.
#! @param this The value to compare against.
#! @param other The value to compare with.
#! @returns Whether the two are equal.
def equals(&this, other: &self): bool;
}
#! Compares two values of the same type through its `equals`, as `==` does.
#! @tparam T The type of the values.
#! @param first The value to compare against.
#! @param second The value to compare with.
#! @returns Whether the two are equal.
#! @remarks A type that is not `equatable` is an error where it is compared, as
#! it is with `==`, since its bytes may differ where its values do not,
#! as the padding between fields does.
pub def equals<T>(first: &T, second: &T): bool {
if const !(T is equatable) {
compile_error("`equals` compares values through `equatable`, which this type does not implement, as `==` does.");
}
return first.equals(second);
}