Pattern matching on method params #6914
-
Background and motivationOften times when dealing with methods that would go through a set of switch cases, we end up creating helper methods to react to each one of the possibilities: public enum Switcheable
{
S1,
S2
}
public void Do(Switcheable s)
{
switch(s)
{
case Switcheable.S1:
DoS1();
break;
case Switcheable.S2:
DoS2();
break;
}
} API ProposalWhat about introducing pattern matching on params? public void Do(Switcheable s?) when s is S1 {...}
public void Do(Switcheable s?) when s is S2 {...}
public void Do(Switcheable s?) when s is null {... I know this needs much refinement } The compiler would convert this into something more like the original example with cases. API UsageAs in the proposal. But also generalizing to all sorts of available pattern matchers public void Do(int x) when x>2 { } Alternative DesignsNo response RisksYou tell me :) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Beta Was this translation helpful? Give feedback.
-
See: #1468 |
Beta Was this translation helpful? Give feedback.
-
At the risk of going off at a tangent, I'd far prefer C# adopted something akin to F#'s "pattern matching functions", so I could write something like: public void Do(Switcheable s) => {
Switcheable.S1 => DoS1(),
Switcheable.S2 => DoS2(),
...
}; |
Beta Was this translation helpful? Give feedback.
See: #1468