-
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
Comparators should return num instead of int #1720
Comments
Set owner to [email protected]. |
I'm curious when you see unnecessary conversions and checked mode errors. |
This comment was originally written by @seaneagan On the VM at least, in checked mode, returning a num or double from a comparator which has return type int should throw a type error. Comparing nums or doubles using subtraction would be the main use case for returning num instead of int, though I guess the suggested way to do this would instead be: double1.compareTo(double2) in which case, forget about this bug. It might be good to add something to the Comparable documentation about this. |
This comment was originally written by [email protected] I've been thinking about moving away from 3-valued comparators, or at least offering other options (individual <, <=, ==, >=, >). Three-valued comparators have caused many subtle bugs over the years. Dart is impervious to some of them, due it to its arbitrary precision approach to int arithmetic. But three-valued comparisons are more costly than two, and violate the principle that you shouldn't have to pay for more than you use. I'm thinking about this issue in conjunction with other collections issues. |
Re-assigning, as jjb isn't here any more. Set owner to @lrhn. |
We are not going to change compareTo to return num. I share Josh's discomfort with the three-value comparators, but the alternatives aren't better. The problem here is that there are different use-cases for comparing. So we want both. Added NotPlanned label. |
This issue was originally filed by @seaneagan
The only thing that matters about the return type of a comparator is whether it is less than, equal to, or greater than zero. Thus, it should be typed num to avoid unnecessary conversions and checked mode errors.
// see issue #1492
Comparable#compareTo
num compareTo(T other);
typedef num Comparator<T extends Comparable>(T other)
List#sort
sort([Comparator<E>]);
The text was updated successfully, but these errors were encountered: