According to the Mojo Reference on Interval it does not implement the Comparable trait, even though it implements the required methods (__lt__ and __eq__). Why is that?
This becomes apparent when trying to sort a list of intervals: calling sort on a list of Interval objects results in an error (no matching function in call to 'sort').
To validate my understanding, I created a small wrapper type around Interval. When I explicitly declare that this wrapper implements Comparable, sorting works as expected. If I omit Comparable, I get the same error as with plain Interval, despite defining lt and eq.
Here is a minimal example illustrating the behavior:
from collections.interval import Interval
@fieldwise_init
struct IntervalWrapper(Copyable, Movable, Comparable):
var interval: Interval[Int]
fn __lt__(self, other: IntervalWrapper) -> Bool:
return self.interval < other.interval
fn __eq__(self, other: IntervalWrapper) -> Bool:
return self.interval == other.interval
fn main() raises:
var intervals = [IntervalWrapper(Interval(1,5)), IntervalWrapper(Interval(0,3)), IntervalWrapper(Interval(4,6))]
sort(intervals)
var pure_intervals = [Interval(1,5), Interval(0,3), Interval(4,6)]
sort(pure_intervals)