Repository navigation
Constraint type inference #6930
|
This is something that has been on many people's wish lists for a while, but now with the addition of generic math it has gained a new use case. Lets say I want to define a generic 3d vector interface: As well as a I then want to define a cross product method that works for any vector, as long as the numeric constraints are met: Perfect! Lets try it out: So close:
So instead you have to write the following: Which is not something we want to type all the time. We want C# to infer the types for us. An alternative would be to change the method signature to So I suggest the language team to reconsider constaint type inference. Right now generic math is unable to reach its full potential without it. Vectors and Matrices, Complex numbers and Quaternions, Points, Sizes and Rectangles, these are all multi value math structures that can greatly benefit from this feature. |
Replies: 5 comments 13 replies
|
Related/duplicates: #110, #478, #741, #997, dotnet/roslyn#5023 and dotnet/roslyn#15166. And here's a comment describing why it's problematic to do. |
|
I'm building a library with some complicated constraints. Due to this lack of automatic inference, some of the things I want to do have become undo-able because I can't expect the caller to have to put in my complicated generic arguments. See #8205 |
|
With the introduction of |
|
Partial type inference is being considered (#8967), which would allow syntax something like: var v3 = Vector.Cross<_, _>(v1, v2);This avoids the compatibility issue since the |
|
I really like the idea of an implicit opt-in mentioned here dotnet/roslyn#7850 (comment)
From a library author perspective, this feature should be as easy to use for consumers as possible. Having to use something like |
Related/duplicates: #110, #478, #741, #997, dotnet/roslyn#5023 and dotnet/roslyn#15166.
And here's a comment describing why it's problematic to do.