Skip to content
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

Suggestion: allow use of non-default constructors in generics #3244

Closed
msauper opened this issue Mar 2, 2020 · 6 comments
Closed

Suggestion: allow use of non-default constructors in generics #3244

msauper opened this issue Mar 2, 2020 · 6 comments

Comments

@msauper
Copy link

msauper commented Mar 2, 2020

Have you considered allowing a constructor prototype to be defined in the where clause of a generic. This would further constrain the type to have a constructor of the given format.

T GetFoo< T >(int i) where T : new(int i) // or just new(int)
{
return new T(5);
}

@GrabYourPitchforks
Copy link
Member

(Not speaking for the language team, just speaking as a partial owner of the System.Reflection namespace.)

This would require runtime support if we wanted to make it efficient. I have some ideas for how we could plumb this through the runtime and reflection APIs in a high-performance fashion, so in theory this should be doable. But I'm having trouble seeing how this would be viable if targeting a runtime that doesn't natively expose these efficient APIs.

@Unknown6656
Copy link
Contributor

@GrabYourPitchforks Is there a possibility that this could be made available in .NET5?
I heard the rumours that .NET5 will be partially re-written from the ground-up. However, I'm not sure whether that concerns the libraries or the runtime. I'm furthermore unsure, how much of a breaking change this proposal will be for the runtime :-/

@Joe4evr
Copy link
Contributor

Joe4evr commented Mar 2, 2020

Duplicate of #769.

@GrabYourPitchforks
Copy link
Member

@Unknown6656 The API portion would be tracked by dotnet/runtime#31895 and dotnet/runtime#23716. In theory you'd be able to use the public APIs even without language support. They're just APIs, after all.

I'm not on the language team so can't speak for their plans in this area.

@Richiban
Copy link

Richiban commented Mar 3, 2020

This would be covered by the Shapes proposal (#164), where generic constraints should cover every type of member of a type, including constructors.

@YairHalberstadt
Copy link
Contributor

Closing as duplicate of #769.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants