M
Modular8mo ago
bunny

Trait `Comparable`

I looked around, but do not see an answer so far. Does Mojo have a Trait like Comparable to indicate that a Struct has implemented all of the comparison dunder methods? Something like:
trait Comparable:
# == equal
fn __eq__(self, other: Self) -> Bool: ...
# != not equal
fn __ne__(self, other: Self) -> Bool: ...
# > greater than
fn __gt__(self, other: Self) -> Bool: ...
# < less than
fn __lt__(self, other: Self) -> Bool: ...
# >= greater than or equal
fn __ge__(self, other: Self) -> Bool: ...
# <= less than or equal
fn __le__(self, other: Self) -> Bool: ...
trait Comparable:
# == equal
fn __eq__(self, other: Self) -> Bool: ...
# != not equal
fn __ne__(self, other: Self) -> Bool: ...
# > greater than
fn __gt__(self, other: Self) -> Bool: ...
# < less than
fn __lt__(self, other: Self) -> Bool: ...
# >= greater than or equal
fn __ge__(self, other: Self) -> Bool: ...
# <= less than or equal
fn __le__(self, other: Self) -> Bool: ...
10 Replies
Ilya I. Lubenets
EqualityComparable | Modular Docs
A type which can be compared for equality with other instances of itself.
bunny
bunnyOP8mo ago
erk.... ty!
bunny
bunnyOP8mo ago
search tool in docs site led me astray:
No description
bunny
bunnyOP8mo ago
Because it went to "similar words" before "word found inside longer compound CamelCase term" I didn't look a ton deeper there. Derp. oh, that's just for equality... just 2 of the 6 comparators: == and !=
sora
sora8mo ago
I think tons of design work still needed to have such a trait
Ilya I. Lubenets
GitHub
[stdlib] add Comparable trait by helehex · Pull Request #2517 · m...
adds a Comparable trait for comparison testing conformance. explicitly conform FloatLiteral, IntLiteral, Int, and Set.
Ilya I. Lubenets
this PR was merged internally, so I'm pretty sure nightly will be updated with this
bunny
bunnyOP8mo ago
🎉 ty ❤️
Ehsan M. Kermani (Modular)
I'm a fan composition so instead we can have PartialOrd trait which can make up Comparable by composing EquallyComparable
Joe
Joe8mo ago
We landed Comparable trait for now in today's nightly
Want results from more Discord servers?
Add your server