-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Allow min
and max
to work with Comparable's like DateTime
#41902
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
Comments
Note that There is no convenient method in the core library, but |
It seems > and < aren't implemented because compareTo() and == have different behavior. == not only cares about the time the DateTime happens but also about the timezone that's set. Looking a bit more at the issue it seems the problem is the max working on Is there a good reason for |
min
and max
to work with Comparable's like DateTime
/cc @lrhn for library change request. |
I'd prefer to not make I'd rather add static class Comparable<T> {
static V min<V extends Comparable<V>>(V first, V second) =>
first.compareTo(second) <= 0 ? first : second;
static V max<V extends Comparable<V>>(V first, V second) =>
first.compareTo(second) >= 0 ? first : second;
... Maybe even |
Okay, then it makes sense to have the functionality for min/max with Comparables outside of the math package. How about a getter on |
That is something I'm considering adding, potentially as part of package:collection. |
I did try to define this as an extension:
For some reason I don't understand the following test fails:
With the error |
It was my thought too that we should be able to use an extension: extension MaxCompare<T extends Comparable<T>> on Iterable<T> {
T get max {
return reduce((a, b) => a.compareTo(b) >= 0 ? a : b);
}
}
main() {
print([Duration(minutes:1), Duration(hours: 2), Duration(seconds: 3)].max); // 2:00:00.000000
} @lrhn what is unfortunate about this is that it does not work for
|
Yeah, it's annoying that If/when we get variance support, the |
I thought you could do extension MaxCompare<U extends T, T extends Comparable<T>> on Iterable<U> {
// ...
} but apparently that does not really do anything, which is a pity, so essentially you can't do this in a safe manner and forced to do extension MaxCompare<T extends Comparable> on Iterable<T> {
// ...
} |
At the moment if you try to Dart should instead throw an accurate and much more appropriate exception as this behaviour is rather confusing. |
See also #3176 "min, max, and clamp apply to all Comparables" |
We have https://pub.dev/documentation/collection/latest/collection/IterableComparableExtension.html I don't think we plan to move the functionality into the SDK, but we can reopen this issue if those plans change. |
I have a list of DateTime string and want to know the latest DateTime in my list. If DateTime would not only provide
.isBefore()
and.isAfter()
I should be able to get the latest DateTime in my list with max (from dart:math).While I do grant that
.isBefore()
and.isAfter()
are normally more readable, they aren't as easy to use with existing functionality likemax
andhim
so it would be useful to have the syntax for both.The text was updated successfully, but these errors were encountered: