dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.59k forks source link

int doesn't implement Comparable<int> #26279

Open Hixie opened 8 years ago

floitschG commented 8 years ago

That's because it implements Comparable<num>.

It's 1am here, so I might be completely off, but I don't see a good solution here. You want to be able to compare doubles with integers, and if int implemented Comparable<int> it would restrict the compareTo to ints.

lrhn commented 8 years ago

The problem with Comparable is that the type parameter occurs contravariantly. We allow you to assign a Foo<int> to a Foo<num> variable because it's safe when the type parameter occurs in covariant positions, and most of them do.

For Comparable its the other way around, a Comparable<num> can be used as a substitute for a Comparable<int> everywhere, but the type system isn't able to recognize that.

lrhn commented 3 months ago

If we get variance annotations, we can make Comparable contravariant, so that Comparable<num> is assignable to Comparable<int>. Until that, there is no chance of any change.