-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed as not planned
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Numerics
Milestone
Description
Background and Motivation
Please make it easier to min/max/clamp any classes.
Proposed API
Right now the Math.Min / Max / Clamp methods only work with a small number of built in data types. A generic version of these methods should be introduced on the Comparer<T>
base class.
public T Comparer<T>.Min(T Value1, T Value2);
public T Comparer<T>.Max(T Value1, T Value2);
public T Comparer<T>.Clamp(T Value, T Min, T Max);
public static T Comparer.Min(T Value1, T Value2) => Comparer<T>.Default.Min(Value1, Value2);
public static T Comparer.Max(T Value1, T Value2) => Comparer<T>.Default.Max(Value1, Value2);
public static T Comparer.Clamp(T Value, T Min, T Max) => Comparer<T>.Default.Clamp(Value1, Min, Max);
Example Implementation
public static class ComparerExtensions {
public static T Min<T>(this Comparer<T> This, T Value1, T Value2) {
var ret = This.Compare(Value1, Value2) <= 0
? Value1
: Value2
;
return ret;
}
public static T Max<T>(this Comparer<T> This, T Value1, T Value2) {
var ret = This.Compare(Value1, Value2) >= 0
? Value1
: Value2
;
return ret;
}
public static T Clamp<T>(this Comparer<T> This, T Value, T Min, T Max) {
var ret = Value;
if (This.Compare(Value, Min) < 0) {
ret = Min;
} else if (This.Compare(Value, Max) > 0) {
ret = Max;
}
return ret;
}
}
Usage Examples
Comparer.Clamp(MyValue, TimeSpan.Zero, TimeSpan.FromSeconds(30))
Alternative Designs
This could just be ignored.
Risks
None.
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Numerics