Skip to content
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

Closed
DartBot opened this issue Feb 16, 2012 · 6 comments
Closed

Comparators should return num instead of int #1720

DartBot opened this issue Feb 16, 2012 · 6 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Feb 16, 2012

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>]);

@sethladd
Copy link
Contributor

Set owner to [email protected].
Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@rakudrama
Copy link
Member

I'm curious when you see unnecessary conversions and checked mode errors.
I have not had this problem myself.
In general you should not compare doubles by doing subtraction as it behaves badly for infinities.

@DartBot
Copy link
Author

DartBot commented Feb 17, 2012

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)
num1.compareTo(num2)

in which case, forget about this bug.

It might be good to add something to the Comparable documentation about this.

@DartBot
Copy link
Author

DartBot commented Feb 17, 2012

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.

@alan-knight
Copy link
Contributor

Re-assigning, as jjb isn't here any more.


Set owner to @lrhn.

@lrhn
Copy link
Member

lrhn commented Aug 30, 2013

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 only real improvement we could do was to return an enum (GREATER,SMALLER,EQUAL) instead of an int, but I don't think it's enough of an improvement to warrant the change.

The problem here is that there are different use-cases for comparing.
When you just have two objects, you may just want to check <, <=, >= or >, and you can often do that cheaper than computing the full compareTo result.
On the other hand, when you are sorting or otherwise ordering (e.g., a tree-based data structure), you very much want the full three-option result in a single operation, and having to do < and then > (or == or reversed < again) will be more expensive.

So we want both.
Preferably I would have both the comparison operators and the compareTo operation on comparable types, so that you can pick the one you need, and we can optimize each one individually. And they should be compatible if possible (coughNaNcough).
Not everbody agrees on that, though, which is why we don't have "<" declared on the comparable interface. :)


Added NotPlanned label.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-not-planned Closed as we don't intend to take action on the reported issue labels Aug 30, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants