Discard/Ignore Generic Parameter #8212
Unanswered
thomhurst
asked this question in
Language Ideas
Replies: 2 comments 3 replies
-
Would be cool if .NET supported generic wildcards (especially bounded wildcards). It would require quite a bit of runtime work, though. |
Beta Was this translation helpful? Give feedback.
0 replies
-
If the generic can still be constrained after omitting parameters, we can merge method overloads that have both generic and Type parameters into one. Enum.GetNames<TaskStatus>();
Enum.GetNames(typeof(TaskStatus)); string[] GetNames<T = null>(Type? type = null) where T : struct, Enum
{
type ??= typeof(T);
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm raising this as an idea to help in my scenario as the better type inference proposal is unlikely to be implemented due to breaking changes.
If I want to utilise generics but I don't care about, and will not use one or more types in a method, then the option to discard/ignore it would be great. This would mean when calling that method, we also don't need to provide those bits of type information.
Given the below code, even though we only care about the key, we have to declare the value as a generic constraint still and this means the user has to provide that information when calling the method, even though we don't care about it.
We could specify a generic type with a
?
to state that we do not need that type information and will not be using it, and that it can match any type.This shouldn't cause any breakages afaik because we wouldn't be able to declare another method with the same number of generic arguments because we'd get a conflict.
For methods / properties where we either would get returned that generic type, or would normally have to pass it in, could we just make those methods uncallable and not shown to intellisense?
Beta Was this translation helpful? Give feedback.
All reactions