1. 15

  2. 4

    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

    Oh my god, yes!!! And there are tuples too? *swoons*

    1. 2

      yep and the tuples deconstruct as well.

      1. 1

        Yes–pattern matching always struck me as one of the things I wish I had from the ML family.

        From the outside, it’s pretty neat how they’ve kept up work on the language over time.

        1. 1

          The “patterns” still seem to be restricted to type test … which are the first thing people tell new developers not to do in languages with pattern matching.

          1. 3

            Well, they enable (and arguably encourage) switching on type, yes… but they also let you switch on additional conditions.

            See this excerpt:

            case Rectangle s when (s.Length == s.Height):
                WriteLine($"{s.Length} x {s.Height} square");
            case Rectangle r:
                WriteLine($"{r.Length} x {r.Height} rectangle");

            It’s not as good as Haskell, F#, SML, Scala, OCaml, or many other langauges… but it’s much better than Java and C++.

            This is an important development for conservative, mainstream, “enterprisey” languages.

          2. 1

            Does it check you’ve covered all the cases?

            1. 2

              It seems there is a mandatory default and null case, so in a sense all cases are covered… but I have a feeling that’s not what you meant.

              C# does not have ADTs, so it’s not exactly clear what “covering all cases” would mean.

              1. 1

                Yeah, I was afraid of that. Kinda reinforces the notion that you can’t just bolt on FP to a pre-existing lang and call it done.