Interval not implementing 'Comparable' trait

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)


I think this should be an issue on GitHub

1 Like

Implicit trait conformance was removed a while back and I guess that the Interval stuff never got updated to explicitly conform. @melodyogonna is right, this should just be a github issue.

Thank you, I will create a GitHub Issue.

1 Like

Here you go: [stdlib][fix] Add `Comparable` trait to `Interval` by christoph-schlumpf · Pull Request #5692 · modular/modular · GitHub