-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
API Proposal: Generic LINQ Numeric Operators #64031
Comments
Tagging subscribers to this area: @dotnet/area-system-linq Issue DetailsBackground and motivationWith the introduction of generic operators it's expected to be able to use LINQ over numeric sequences in a generic manner. This relates to #63732 where new constraints are being discussed for API Proposalnamespace System.Linq
{
public static class Enumerable
{
public static T Sum<T>(this IEnumerable<T> source) where T : IAdditionOperators<T, T, T>;
public static T Min<T>(this IEnumerable<T> source) where T : ..;
public static T Max<T>(this IEnumerable<T> source) where T : ..;
// The result value is most likely floating-point. Should it be generic?
// If so, is it possible for type inference to pick an appropriate type?
public static T Average<T>(this IEnumerable<T> source) where T : ..;
// ..
}
} API UsageIEnumerable<T> numericSeq = e;
T sum = numericSeq.Sum(); Alternative DesignsNo response RisksNo response
|
Echoing @tannergooding's comments in #63732 (comment) wouldn't adding such overloads break overload resolution? |
I wouldn't think so. The current overloads (taking |
I specifically had this need considering how to represent a sum of time intervals operation for translation to SQL with EF Core. A generic Sum method would be perfect for that (TimeSpan would also to implement the appropriate interfaces as well). Of course we'd need queryable versions of these operators. |
The selector-based overloads (example) seem to be missing from the API proposal. public static T Sum<TSource, T> (this IEnumerable<TSource> source, Func<TSource, T> selector)
where T : IAdditionOperators<T, T, T>, IAdditiveIdentity<T, T>; Especially for these, perhaps a more descriptive name for |
Why hasn't this been done already? Genuine question. We've had generic math for a few years now. Was there a reason implemented right away? |
@aradalvand that kind of comment isn't helpful; engineering resources are always limited, and decisions must be made on what's more important. I do agree this issue needs to be done at some point, but given that LINQ operators exist for the major numeric types, it doesn't seem very urgent. This seems to be confirmed by the fact that there are only 30 votes on this issue since it was opened 2.5 years ago. |
@roji I may have worded that badly; I was just curious to know if there's any other problem currently blocking this or something. Similar to how, for example, there doesn't seem to be a consensus on whether |
The short answer is resourcing, somebody needs to take the time to prototype generic math methods and importantly examine how new overloads might interact with existing ones in terms of breaking changes etc. It's also not entirely clear to me how generic math (or static abstracts in general) would interact with the |
Background and motivation
With the introduction of generic operators it's expected to be able to use LINQ over numeric sequences in a generic manner.
Additionally this can be considered for extensions on other list-like types.
Relates to #63732
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: