Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fall through in pattern matching

currently in c#7 (version 15.3.4) following code is valid to compile but both variables are legitimately unusable.

switch(fruit)
{
    case Apple apple:
    case Orange orange:
    // impossible to use apple or orange
    break;
    case Banana banana:
    break;
}

If you try to use them, you get familiar error, variable might not be initialized before accessing.

Some times in pattern matching you don't care about exact type, as long as that type is in category that you want. here only apples and oranges as an example.

List<Fruit> applesAndOranges = new List<Fruit>();
switch(fruit)
{
    case Fruit X when X is Apple || X is Orange:
    applesAndOranges.Add(X);
    break;
    case Banana banana:
    break;
}

Are there better approaches?

like image 639
M.kazem Akhgary Avatar asked Sep 16 '17 14:09

M.kazem Akhgary


People also ask

What is the pattern in the match expression?

For example, in the match expression, the pattern is what follows the pipe symbol. match expression with | pattern [ when condition ] -> result-expression ... Each pattern acts as a rule for transforming input in some way. In the match expression, each pattern is examined in turn to see if the input data is compatible with the pattern.

What is pattern matching in Java?

Pattern Matching. Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the switch statement in Java and it can likewise be used in place of a series of if/else statements.

What is the purpose of each pattern?

Each pattern acts as a rule for transforming input in some way. In the match expression, each pattern is examined in turn to see if the input data is compatible with the pattern. If a match is found, the result expression is executed. If a match is not found, the next pattern rule is tested.

What is the use of pattern in F?

They are used throughout F# to compare data with a logical structure or structures, decompose data into constituent parts, or extract information from data in various ways. Patterns are used in many language constructs, such as the match expression.


1 Answers

You can use discards if you don't like to make garbage local variables in current region. then you can use switched variable directly. you may need additional cast if switched variable is of super class like object or something else.

List<Fruit> applesAndOranges = new List<Fruit>();
switch(fruit)
{
    case Apple _:
    case Orange _:
    applesAndOranges.Add(fruit);
    break;
    case Banana banana:
    break;
}

I don't know how pattern matching is compiled. if it makes use of jump tables then this approach could be also a little faster. performance is not my concern though. this is more readable.

I'm already pleased by both solutions, so this is Q&A post that I liked to share.

like image 82
M.kazem Akhgary Avatar answered Oct 03 '22 11:10

M.kazem Akhgary