[Proposal]: Extend Generic new() constraints #9425
Replies: 3 comments 4 replies
-
exactly because theres already a workaround you mentioned this idea isnt going to gain any traction. Additionally theres plan to make it possible to add ad hoc interfaces via extensions (or more specifically 'roles') and if this will work for |
Beta Was this translation helpful? Give feedback.
-
The bigger issue is that without some kind of runtime changes there really isn't support to describe this constraint in metadata. C# could do something custom like F# did, but that means that other languages can ignore the constraint and call the method with incorrect type arguments and the runtime won't do anything to prevent that. |
Beta Was this translation helpful? Give feedback.
-
dup of #8542 |
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.
-
Lets assume the following interfaces and classes
It would be nice to allow construct type constraints such as the following.
Full code:
TKey could for example be a Guid and for TRole I want to have a constructor that takes for example an id and a role name or even more parameters. It's cleaner, less code and I don't have to make the Property setter internal or public.
With the "relativly" new static interface members the following workaround archives essentially the same which means It probably can be added "simply" by using source code generation.
Sorry in case it's a duplicate but I couldn't find a similar discussion.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions