Hacker News new | ask | show | jobs
by DropbearRob 3588 days ago
Im so excited by this in particular

"Switch statements with patterns

We’re generalizing the switch statement so that:

•You can switch on any type (not just primitive types)

•Patterns can be used in case clauses

•Case clauses can have additional conditions on them

Here’s a simple example:

switch(shape)

{

    case Circle c:

        WriteLine($"circle with radius {c.Radius}");

        break;

    case Rectangle s when (s.Length == s.Height):

        WriteLine($"{s.Length} x {s.Height} square");

        break;

    case Rectangle r:

        WriteLine($"{r.Length} x {r.Height} rectangle");

        break;

    default:

        WriteLine("<unknown shape>");

        break;

    case null:

        throw new ArgumentNullException(nameof(shape));
}

There are several things to note about this newly extended switch statement:

•The order of case clauses now matters: Just like catch

clauses, the case clauses are no longer necessarily disjoint, and the first one that matches gets picked. It’s therefore important that the square case comes before the rectangle case above. Also, just like with catch clauses, the compiler will help you by flagging obvious cases that can never be reached. Before this you couldn’t ever tell the order of evaluation, so this is not a breaking change of behavior.

•The default clause is always evaluated last: Even though the null case above comes last, it will be checked before the default clause is picked. This is for compatibility with existing switch semantics. However, good practice would usually have you put the default clause at the end.

•The null clause at the end is not unreachable: This is because type patterns follow the example of the current is expression and do not match null. This ensures that null values aren’t accidentally snapped up by whichever type pattern happens to come first; you have to be more explicit about how to handle them (or leave them for the default clause).

Pattern variables introduced by a case ...: label are in scope only in the corresponding switch section."