Comments (13)
This comment was originally written by [email protected]
I have to agree, since you also note in Dart's documentation that "No more someString.equals("something").", how about no "a.compareTo(b)"? It doesn't make any sense.
from sdk.
This comment was originally written by [email protected]
One complication is you would need to define <, >, >=, and <= -- plus you have to worry about inconsistencies with ==/!=. There are a few ways that could be handled -- for example, if a class implements Comparable, the compiler/VM could synthesize the rest if they weren't defined (you could still cause inconsistent behavior by defining them and doing so inconsistently).
Removed Type-Defect label.
Added Type-Enhancement label.
from sdk.
This comment was originally written by [email protected]
I think how Python does this, [http://www.python.org/dev/peps/pep-0207/ PEP 207: Rich Comparisons], is relevant here.
from sdk.
This comment was originally written by [email protected]
@ jat:
It's been awhile since I've been in a university math course, but wouldn't defining less than and equals be enough to synthesize the remaining operators? Are there any cases where something is not less than and not equals, yet it would be incorrect to infer that it's greater?
Also, as far as causing inconsistent behavior by defining inconsistently - wouldn't it be the same with implementing a compareTo method? What's to prevent me from always returning a -1 from compareTo, regardless of value?
The advantage of implementing the operators is that it's far more clear what the logic is being expressed. If I say that string a compared to string b is -1, that's semantically meaningless. As far as I knew, the ONLY reason Java went this route is that there is no operator overloading.
C'mon, if it's just sacred cattle, let's slaughter it. =)
from sdk.
This comment was originally written by [email protected]
If interfaces allowed for default method implementations, Comparable operators could be handled something like this:
interface Comparable {
int compareTo(Comparable other);
bool operator < (Comparable other) => compareTo(other) < 0;
bool operator <= (Comparable other) => compareTo(other) <= 0;
bool operator > (Comparable other) => compareTo(other) > 0;
bool operator >= (Comparable other) => compareTo(other) >= 0;
...
}
This is effectively the same as having the compiler "synthesize the rest", but gives the same tool to the programmer for use in other circumstances. In some ways, this proposal is similar to that suggested in issue #33
from sdk.
This comment was originally written by [email protected]
Inheriting implementation from an interface seems kind of tricky -- how do you avoid the typical multiple-inheritance problem? For example:
class Foo { bool foo() => ... }
interface Int1 { bool foo() => ... }
interface Int2 { bool foo() => ... }
class Bar extends Foo implements Int1, Int2 {...}
Which foo() does Bar get? What if there is an intermediate superclass between Foo and Bar? This can certainly be solved, but I wonder if there are going to implications from this.
It seems like mixins might be a safer approach.
Added Area-Language label.
from sdk.
This comment was originally written by [email protected]
Added Triaged label.
from sdk.
This comment was originally written by [email protected]
In response to #4, you could even do it all with just <.
a == b -> !(a < b) && !(b < a);
a != b -> !(a == b)
a > b -> b < a;
and so on...
from sdk.
Removed Area-Language label.
Added Area-Library label.
from sdk.
We would have liked not to have a special "compareTo" method, but unfortunately doubles make this impossible.
NaN == NaN // false
NaN.compareTo(NaN) // 0
NaN > Infinity // false
NaN < Infinity // false
NaN.compareTo(Infinity) // > 0
-0.0 == 0.0 // true
(-0.0).compareTo(0.0) // < 0
compareTo must be reflexive, antisymmetric and transitive. '<' thus doesn't fit the bill...
Alternatively one could have a a.isLower(b) (or similar) which would avoid up to two comparisons for the common 'if (a.compareTo(b) < 0)...' case.
Added WontFix label.
from sdk.
This comment was originally written by [email protected]
Right, but the comparison operators can be implemented in terms of compareTo, if we add something like mixins Comparable can provide default implementations of them, so if you implement compareTo you get all the rest for free.
from sdk.
This comment was originally written by [email protected]
@floitsch
looks to me like sacrificing a well working solution in favour of a very academic case.
NaN > Infinity -> false may lead to the same delusion as NaN > Infinity = true.
I don't see why the latter is worse in real cases. But I see a problem if a, b claim to be comparable, but a < b gives me an error.
By the way Ruby and Swift don't seem to have a problem with this behavior.
from sdk.
This comment was originally written by [email protected]
So this won't be fixed because Comparable must be able to handle partial orderings, and adding those comparison operators breaks that. Right?
double.compareTo already produces a total ordering, though, and it considers NaN to be greater than +Inf. So it would be no more broken as long as the Comparable interface's operators were overridden by the ones defined on double.
As an alternative, if Comparable.compareTo could return NaN, then double.compareTo could produce the right results -- and then Comparable's comparison operators would produce the right results too.
So I'm not sure what the issue is.
from sdk.
Related Issues (20)
- Encapsulate field assist miss-types the constructor field if the type is from an aliased import HOT 1
- Encapsulate field assist doesn't move override annotations HOT 1
- [Macro] [Analyzer] [Error] [Invalid argument(s): Unresolved identifier: int HOT 7
- An unexpected error was encountered by the Analysis Server. HOT 1
- Analyzer `--fatal-infos` flag issues HOT 2
- [analyzer] Provide a `ResourceProvider` that takes a `FileSystem` from the file package HOT 8
- Build issue with js_interop `isA` HOT 2
- Mark `dartExecutable` parameter as `required` in `DartDevelopmentServiceLauncher.start` HOT 1
- `_NativeSocket.startConnect` is possibly throwing `SocketException`s that cannot be caught with `try` HOT 7
- [wildcards] Semantic Highlighting - consider styling wildcards HOT 9
- Dart vm `RegExp` doesn't seem to properly match all emojis HOT 2
- [analyzer] how do i get middle of prefix value type? HOT 1
- dart fix --apply throws an error HOT 1
- Extension methods on records are not suggested HOT 3
- Dart should have process for keeping VM RegExp up-to-date with Irregexp
- Converge `package:test_runner` and `package:test`? HOT 6
- `test.py` doesn't report failure in case of `info`-level issue and `--fatal-infos` HOT 2
- Analyzer doesn't warn on duplicate extensions from different part files HOT 4
- VSCode Dart Extension "An error occurred while handling initialized notification" HOT 8
- Missing `continue_without_label_in_case` error when switch statement is in a for loop. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.