Proposal: Switch pattern for generic Type #5687
-
This is what's possible today: public T As<T>()
{
T x = default;
return x switch
{
int when _value is T i => i,
long when _value is T l => l,
_ => default
}
} This works fine while Something like this would therefore be appreciated: public T As<T>()
{
return T switch
{
int when _value is T i => i,
long when _value is T l => l,
_ => default
}
} I simplified the code a lot, I know it isn't very usefull in this state, but it serves the purpose of demonstrating the issue |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
See eg #356. As to your example, I appreciate you have "simplified the code a lot", but as it stands, your public T As<T>() => _value is T x ? x : default; Or, if you prefer a switch expression: public T As<T>() => _value switch {
T i => i,
_ => default
}; There is no need to create |
Beta Was this translation helpful? Give feedback.
-
continues in #356 |
Beta Was this translation helpful? Give feedback.
continues in #356