Replies: 3 comments
-
I think this might already be addressed by shapes (Issue #164) public shape SAddDivide<T>
{
static T operator +(T t1, T t2);
static T operator /(T t1, double d);
} public static class IEnumerableExtensions
{
public static double Median(this IEnumerable<T> values) where T : struct, SAddDivide<T>
{
// ...
}
} |
Beta Was this translation helpful? Give feedback.
-
Indeed. Searching the issues for "where" yielded far too many results unfortunately (surprise) so it was hard to find similar issues. I believe the new() syntax results in a call to Activator.CreateInstance which already supports an object array being passed in so new(string,int,double,foo) should be possible with a compile-time check of types in usage and passing that in as an object array. Admittedly, not something that's needed very often. |
Beta Was this translation helpful? Give feedback.
-
This is intended to be addressed by type classes. See #110. The shapes issue is an interested exploration of that space too. |
Beta Was this translation helpful? Give feedback.
-
Problem
The current extent of the generic type "where" condition does not have a wide scope supporting only a base type, interfaces, class or struct specifier and a possible parameter-less constructor.
We can do things like this;
Which is great but often comes up a little short. What if we want to specify type of T has a constructor that takes a string which we want to use? What if we'd like to work with some operators?
For example, create an IEnumerableExtension class with a Median method that requires an addition and division operator.
Right now we'd need to explicitly implement that method for every numeric type - we can't make it generic.
Solution
Expand on the ability to specify the restrictions/expectations of our generic type using the where clause.
For example;
And here's that median implementation made possible;
Conclusion
I'm not sure how feasible the operators would be but surely the constructor specifier should be fairly straight-forward (famous last words)
Beta Was this translation helpful? Give feedback.
All reactions