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

Please implement comparison (e.g. <=,<,>,>=) operators for String #608

Closed
DartBot opened this issue Nov 28, 2011 · 4 comments
Closed

Please implement comparison (e.g. <=,<,>,>=) operators for String #608

DartBot opened this issue Nov 28, 2011 · 4 comments
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 Nov 28, 2011

This issue was originally filed by jack.palevich@gmail.com


operator<, operator<=, etc.

They can be implemented in terms of the existing Comparable.compareTo()

(It would be neat if Dart knew to automatically implement the relational operators for any class that implemented Comparable.)

@DartBot
Copy link
Author

DartBot commented Nov 28, 2011

This comment was originally written by drfibonacci@google.com


Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@floitschG
Copy link
Contributor

Removed Area-Language label.
Added Area-Library label.

@DartBot
Copy link
Author

DartBot commented Nov 30, 2011

This comment was originally written by @seaneagan


The suggestion to:

"automatically implement the relational operators for any class that implemented Comparable"

... could be done via issue #33 (mixins). Mixins would have an implicit interface just like classes, so Comparable could be an abstract mixin:

mixin Comparable<T extends Comparable> {
  int compareTo(T other);
  bool operator < (T other) => compareTo(other) < 0;
  bool operator <= (T other) => this < other || this == other;
  bool operator > (T other) => !(this <= other);
  bool operator >= (T other) => !(this < other);
}

interfaces could extend Comparable, and classes could both implement and mixin Comparable, and then merely define compareTo. I would get rid of compareTo though since it makes it ambiguous as to whether <= and >= should use == or compareTo(other) == 0. Instead < could be the abstract method which needs to be implemented, like so:

mixin Comparable<T extends Comparable> {
  bool operator < (T other);
  bool operator <= (T other) => this < other || this == other;
  bool operator > (T other) => !(this <= other);
  bool operator >= (T other) => !(this < other);
}

Then you could do something like:

interface String extends Comparable factory StringFactory {/.../}
class StringFactory extends Comparable implements String {
  //...
  operator < (String other) native;
}

@floitschG
Copy link
Contributor

There are currently no plans to add comparison operators to Strings.
We have (reluctantly) added a compareTo method on Strings to allow sorting, but there isn't really a good natural order on Strings. We don't want to give this impression by adding comparison operators.


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 Sep 3, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
@eernstg eernstg mentioned this issue Oct 24, 2019
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

3 participants