C# 14 - Extensions with different generic parameters on the extension and the method cannot infer the types independently #9714
Replies: 2 comments 15 replies
-
This was discussed many times:
|
Beta Was this translation helpful? Give feedback.
-
Hi there, I do agree, the inability to separately infer the type parameters of the receiver (which should always be possible) is a serious limitation. But this has been sacrificed over backwards compatibility. And although the team signalizes, that there may be work done in the future regarding the problem - I personally think, it will never make it - wich is sad. The team thinks, that partial inference could solve the problem, but it does not: none of the proposals, that have been made until now, is able to truly reduce the number of type parameters - you don't have to specify types, that can be inferred (which is a great feature btw!), but the number of type parameters stays the same (because there is no notion of which non-inferred (actual) type parameter goes actually to which declared type parameter - in a sense all proposals go for positional type parameter matching - maybe that is because type parameters don't have a parameter name). The team also considered a 2-phase inference (which could have solved the problem) - as was specified for extensions in C#3 in the past (but actually something else was implemented) - but considered this a breaking change, which they didn't want (given the vast amount of existing code). Unless that opinion changes, I don't see that 2-phase inference coming. Does anybody know of a general generic implementation of the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We have an
Option<T>
monad (implemented as astruct
) and we would like to have something likeOfType<T>
implemented as an extension on it.So far, you couldn't simply implement such an extension on a generic type because C# does not support partial type inference on generic methods. Something like below was possible, but you always have to give both types, even though
TItem
can be inferred by the given option. And that is utterly inconvenient.There are alternatives you can use to partially get what you want:
Option<T>
directly not as an extension.IOption
(That actually is the wayOfType<T>
is implemented onIEnumerable
)Fast forward to C#14 - now we can define the extension independent of the Method, so I thought now this could finally be done.
So I tried the following:
This can be implemented, and it compiles perfectly fine.
But if I try to use the method, I do get a compilation error:
The error is the same as if the extension wouldn’t exist at all.
error CS1061: 'Option<object>' does not contain a definition for 'DownCast' and no accessible extension method 'DownCast' accepting a first argument of type 'Option<object>' could be found (are you missing a using directive or an assembly reference?).
i checked if it is just an accessibility problem, but if I implement a non-generic extension like "HasValue"
So I have multiple questions, is that a Bug and where could I report that?
I have the bad feeling that this was overlooked because this was not possible before and nobody tried it.
I use the latest preview.
PS:
After Writing all this, I thought I should check if the compiler would find the method if I give it both generic arguments, like in the sad past.
And it worked, this compiles, but it is very say... and totally counter intuitive. Generic type-inference is so bad in C#, and this is really a missed opportunity.
I am convinced that the generic type argument on the extension and the method should be inferred independently. I do not see a reason why you couldn't do that inference independently.
I consider this a major design flaw... did nobody try this?
Beta Was this translation helpful? Give feedback.
All reactions