BT

InfoQ Homepage News C# 9: New `and`, `or`, and `not` Keywords for Pattern Matching

C# 9: New `and`, `or`, and `not` Keywords for Pattern Matching

Bookmarks

Though it may sound like our occasional April Fools Day joke, C# 9 is looking to add and, or, and not to its list of keywords. Specifically, for use in pattern matching.

In order to make pattern matching more flexible and powerful, C#’s designers want to add the concept of conjunctive, disjunctive, and negated patterns. Superficially, they look just like Boolean operations where you want to match on both patterns (conjunctive), either pattern (disjunctive) or not match on a pattern (negated).

And that’s the problem. If you are working with Booleans, the && and || operators would be ambiguous; the compiler wouldn’t be able to determine if they are referring to values or patterns. To illustrate this idea, consider this disjunctive pattern:

if (myBool is true or false)

This would be interpreted as “true if myBool equals true or if myBool equals false”.

If we use the && and || operators for combining patterns, then we get this syntax:

if (myBool is true || false)

But this statement already means “true if myBool equals the result of (true or false)”. Or to simplify it, “true if myBool equals true”. Which is a completely different result than what we’d get with above disjunctive pattern.

Hence the new and, or, and not keywords are necessary to avoid ambiguity. You can learn more about them in Champion "and, or, and not patterns". In the C# 9 feature status they are marked as being merged into the master branch.

One question frequently raised is whether or not the same variable can be declared multiple times. For example,

if ((e1, e2) is (0, int x) or (int x, 0))
{
    M(x);
}

In this theoretical example, the variable x is either e1 or e2. This would be the equivalent of writing,

case (0, int x):
case (int x, 0):
    M(x);

In fact, you would have to declare x in both patterns for x to be definitely assigned. If you used the below code instead, either x or y wouldn’t have a value.

if ((e1, e2) is (0, int x) or (int y, 0))
{
    M(x);
    M(y);
}

In order to address this problem, the current proposal says,

beneath a not or or, pattern variables may not be declared.

This may be reconsidered in future versions of C#.

Rate this Article

Adoption
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.

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

Community comments

  • Operator summary

    by Jonathan Allen /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    This may help to clarify things:

    & | !: Boolean operators over bits

    && || !: Boolean operators over System.Boolean

    and or not: Boolean operators over patterns

  • C# 9: New `and`, `or`, and `not` Keywords for Pattern Matching

    by Daniel Borden /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    And they say "Visual Basic" is a dead language ... are we sure about that? In with the old and out with the new, as I say.

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

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

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.