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:
orcase 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 mutablevar
; - 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.