what to do about Comparable
See original GitHub issueAfter #7395, I changed Comparable
to be invariant.
@jvasileff points out that a bunch of our functions including sort()
and max()
don’t work as well when you have things that indirectly implement comparable. It is possible to rescue all these functions by adding a new type parameter C
and having parameters of type, for example, Element & Comparable<C>
or {Element & Comparable<C>*}
.
However, that’s pretty messy and I don’t see it as acceptable. There’s two reasonable solutions:
-
Make
Comparable
contravariant and remove its self type. -
Taking advantage of #7408, define
Comparable
as follows:shared interface Comparable<Other> of Other given Other satisfies Comparable<in Other> { ... }
Then functions like
sort()
andmax()
just use the same type constraintgiven Other satisfies Comparable<in Other>
.
I like solution 2, assuming that #7408 doesn’t run into some terrible snag.
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
After sleeping on this, I think the cleanest solution is just to:
So I’ve done that. Thanks to @jvasileff for pointing out the problems with making
Comparable
invariant.Right.