-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comparable.compare should be generic #26085
Comments
IIRC @munificent and I looked into this one, the way it's used (as the default comparator for ordered Maps/Sets) it probably needs to be less typed. Something along the lines of: Also CC @leafpetersen |
That would actually work better in my use-case. |
I'm not even sure the suggested generification would work correctly. The the generification of the existing signature (taking How about |
Yup, I think that's the right answer. |
Everyone subscribed: what's the story on this? Still an issue? (Just curious...) |
it appears to still be an issue: https://api.dartlang.org/stable/1.24.2/dart-core/Comparable/compare.html |
The problem with going for int compare<T extends Comparable<T>>(T a, T b) => a.compareTo(b); as the declaration is that it's a recursive type constraint which can make inference hard. On the other hand, the recursively typed declaration will make int compare<T>(Comparable<T> a, T b) => a.compareTo(b); isn't a good alternative - then There are existing uses of the plain Object compare = Comparable.compare; That would not instantiate the function, so I'm not actually sure whether an instantiation of a generic static function is a compile-time constant expression, so if there are any uses of And then there is the problem that inference might pick a type different from // Here we have to be certain that it infers T as `num`, because `int` is! Comparable<int>.
// I don't know it it does, and I don't expect users to be able to predict it.
print(Comparable.compare(2, 3)); Making the change is simple. It will not affect Dart 1 at all (the syntax, it does nothing!), so maybe the analyzer or DDC team can test how the change would affect strong mode? I've made a CL: https://codereview.chromium.org/2959443002 |
The
Comparable.compare
static function currently has no generic annotations, which makes it unusable in strong mode.Comparable.compare/*<T>*/(Comparable/*<T>*/ a, Comparable/*<T>*/ b)
is probably the way to go.The text was updated successfully, but these errors were encountered: