-
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
min, max, and clamp apply to all Comparables #3176
Comments
Added Area-Library, Triaged labels. |
Generic Comparable versions of clamp, min and max would be fine, but they would not give the same results as dart:math's num versions (even if the only difference is on NaN). |
Removed Type-Defect label. |
This comment was originally written by @seaneagan min, max, and clamp (and many others) are available as top-level functions in the contrast package: https://pub.dartlang.org/packages/contrast Of course if they were instance methods then they would propogate type info better. I think the difference with NaN is actually a good reason to make them instance methods (polymorphism), would just need to be documented. |
I imagine the cost of adding these to |
The pub client still won't send analytics with this PR. (This will be enabled in a follow-up). Changes: ``` > git log --format="%C(auto) %h %s" 0764437..35681b0 https://dart.googlesource.com/pub.git/+/35681b01 Analytics (#2778) https://dart.googlesource.com/pub.git/+/9c65d31c Accept 'platforms' property in pubspec.yaml (#3176) https://dart.googlesource.com/pub.git/+/1af0de54 Accept 'screenshots' property in pubspec.yaml (#3174) https://dart.googlesource.com/pub.git/+/19045b95 Tests related to token authentication (#3147) https://dart.googlesource.com/pub.git/+/f36da07f Use applicationConfigHome from package:cli_util (#3164) ``` Diff: https://dart.googlesource.com/pub.git/+/0764437088fd58eb7af779ecef66bab40dfcf2e9~..35681b0126a1fb48bf2062dd09f74296715402c2/ Change-Id: I7313f3125934aba3473b2725074bfc7fd92e25e4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217004 Reviewed-by: Jonas Jensen <[email protected]> Commit-Queue: Sigurd Meldgaard <[email protected]>
It's unclear why this was closed. Can it be re-opened? (Especially given the advent of extensions.) |
The We could add static class Comparable<T> {
// ...
static T min<T extends Comparable<T>>(T v1, T v2) => v1.compareTo(v2) <= 0 ? v1 : v2;
static T max<T extends Comparable<T>>(T v1, T v2) => v1.compareTo(v2) >= 0 ? v1 : v2;
}
extension ClampComparable<T extends Comparable<T>> on T {
T clamp(T min, T max) => compareTo(min) < 0 ? min : compareTo(max) > 0 ? max : this;
} The |
This issue was originally filed by @seaneagan
dart:math (formerly the Math class) has min and max methods which accept and return nums, issue #44 additionally requests a related "clamp" method. These methods should be generalized to accept and return any Comparables.
For such methods to be type safe, they should be generic (issue #254 - Milestone-Later, which would cause type warnings (checked mode errors)
if these methods were made generic after the fact), and Comparable should have a type parameter(issue #1492). They would look something like:
T min<T extends Comparable<T>>(T x, T y);
T max<T extends Comparable<T>>(T x, T y);
T clamp<T extends Comparable<T>>(T x, T min, T max);
Generic method support would not be required though if they were instead instance methods in Comparable (which would also avoid the type argument on calls).
They could also potentially be static methods on Comparable if statics were supported in interfaces (http://dartbug.com/2975 except without restriction to go through a
default class).
The text was updated successfully, but these errors were encountered: