-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Why can't compiler resolve type in a generic method for a non-generic callee? #8273
Comments
In C++ this works because templates are effectively copy-specialized ahead of time for each caller. So it copies the template, substitutes the placeholders and if it compiles, it compiles. In C# generics are metadata driven. The compiler embeds the method generically into the assembly without specializing for any caller. Specialization is done at runtime by the JIT, but as you've already figured, you're reciving the error at compile time, because it's not caller specialized and the compiler can't express the idea of "this method is only called once and when it is, it has the correct type, so it's declared generic but really isn't" in metadata. This also wouldn't be very intuitive, compiler errors caused in method bodies by the caller... |
https://msdn.microsoft.com/en-us/library/c6cyy67b.aspx
http://stackoverflow.com/questions/1208153/c-sharp-generics-compared-to-c-templates
Yes #1911 but C++ is not the best source of inspiration for this. Something usable in .NET today: https://github.com/rsdn/nemerle/wiki/Macros-tutorial |
No because this comes down to a fundamental difference between generics and templates.
The closest analogous features to C++ templates in C# is the proposal of introducing structural typing. This is something I'd love to see us do one day but it's a rather big work item. |
In C++, following code is legit:
In C#, the generics don't allow this:
Why can't compiler resolve it, when there is only one caller, one generic dispatcher and one implicit callee?
Can't generic borrow this feature from C++ templates?
Any chance this can turn into a viable proposal?
The text was updated successfully, but these errors were encountered: