BT

Advanced Pattern Matching Features Removed From C# 7

| by Sergio De Simone Follow 14 Followers on May 13, 2016. Estimated reading time: 2 minutes |

Advanced pattern matching features that were originally expected to be present in C# 7 have been recently excluded from the future branch and will not make it into the next version of the language.

The change of scope for C# 7 pattern matching has already materialized in Roslyn’s GitHub repo. In particular, issue #10866 (“Split the features/patterns branch into two branches for subfeatures in/out C# 7”) and PR #10888 (“Remove evidence of advanced pattern-matching features for C# 7”) thoroughly describe what this change is about.

As InfoQ reported a few weeks ago, pattern matching was going to be one the most appealing new features in C# 7, especially for programmers coming from a F# or Haskell background. Specifically, new pattern matching features were expected to:

  • enhance case blocks by allowing for switching based on the type or range of a variable, e.g. case int x: or case int x when x > 0;
  • add support for destructuring, which would allow developers to kind of tear apart an object into some of its components, when it met given conditions, while also creating local variables to refer to those components. An example of this is provided by the syntax if (person is Professor {Subject is var s, FirstName is "Scott"}).

Now, according to Roslyn issue #10866, both the syntax expression is Type identifier and case Pattern when expression for a few basic pattern forms have been moved to the future branch for inclusion in C# 7. On the contrary, the remaining features have been left in the patterns/features branch, which hosts features “that might be delivered in a later release”.

This means that the more advanced kinds of pattern matching, explained effectively by reddit poster wreckedadvent, will not be available in C# 7, including:

  • recursive pattern forms such as positional patterns (e.g., p is Person("Mickey", *), property patterns (e.g., p is Person {FirstName is "Mickey"}), tuple patterns, wildcard *, etc.
  • the let keyword, supplying immutable vars (e.g., let x = e2 when e2 else stmt;), as opposed to mutable var;
  • the match expression which would allow to write:
      var result = ...
      let message = result match (
        case Success<string> success: success.Result
        case Failure err: err.Message
        case *: "Unknown!"
      );
  • pattern-matching based on user-defined code, such as an user-defined is operator.

There have been a few reactions within the community of C# developers. On the one hand, those more keen on functional programming have expressed their relative deception at the lack of a feature that would have made C# more functional. Other developers, on the contrary, have said themselves not concerned or glad that C# evolution is being managed in a disciplined and controlled way.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

That would have been a nice addition by Barry O'Blarney

Bummer!

Just use F# by Phylos Zero

F# has great pattern matching capabilities and much more. It's available today - no need to wait for C# 8.0.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

2 Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT