C #6 and VB 12 will be added to the pattern match

Source: Internet
Author: User

Another concept originated from functional programming languages is added to the C # and VB camps, which are known as pattern matching. At first glance, pattern matching works like a switch/SELECT statement block, but it has many powerful functions.

Note: The specification of the vb version has not been completed yet, so these examples are mainly from the pattern matching Specification Description in C.

Is or matches Operator

Pattern Matching in. NET is mainly supported by the "is/matches" operator. This unfamiliar operator breaks down a class into multiple components. The following example is created based on the Cartesian record class http://www.infoq.com/news/2014/08/Record-Class (record class) mentioned in Tuesday news reports.

12345 public static bool operator is(Cartesian c, out double x, out double y)    x = c.X;    y = c.Y;    return true;}

This is operator is not only used in its definition class. The following example defines an operator in another way so that it can break down a Cartesian object, match a polar object.

1234567891011 public static class Polar {    public static bool operator is( Cartesian c, out double R, out double Theta)   {        R = Math.Sqrt(c.X*c.X + c.Y*c.Y);        Theta = Math.Atan2(c.Y, c.X);        return c.X != 0 || c.Y != 0;   }}var c = Cartesian(3, 4);if (c is Polar(var R, *))   Console.WriteLine(R);
Type Mode

The simplest mode is the type mode. In essence, it is to try to convert the type and assign it to a variable at the same time. The following is an example of this mode:

12 if (expr is Type v)   { // code using v }
Recursive Mode

Most of the patterns appear as recursive patterns, meaning they are composed of simple patterns. Let's take a look at the example of this mode:

12 var a = new Location(1, 2, 3); //x=1, y=2, z=3if (a is Location(1, var y, *))

This recursive mode includes a constant mode, a var mode, and a wildcard mode.

Constant Mode

In this mode, an attribute can be matched with a constant value. For constant matching, the object. Equals (left, right) method is used to determine whether the two match.

VAR mode

The VAR mode will certainly match successfully. The variables corresponding to this mode will be assigned the value provided when the is operator is called, the variable type is the static type determined by the expression.

Wildcard Mode

The wildcard mode is essentially a var expression, but you do not need to care about the matching results.

Internal implementation

Let's continue to discuss the example of this location class. The compiler will perform the following steps in the compilation phase:

  1. Create variables $ X, $ y, and $ Z
  2. Call the location. Is (A, out $ X, out $ y, out $ Z) method and confirm that the returned result is true.
  3. Constant mode: checks the results of object. Equals ($ X, 1) calls.
  4. VAR mode: assign $ y to Y
  5. Wildcard mode: Ignore $ Z variable
Switch/select case statement Block

The switch block function will be extended, and it will be able to use the pattern matching feature. This actually means you can write the statement as follows:

123 case null:case String scase Location(1, var y, *):
Restrictions

Currently, the range check is not supported in the draft Specification. This means that you cannot write code similar to "A is location (> 0, 1 to 5, <= 10. Currently, element matching is not supported in a list or iterator.

Original article: Pattern Matching in C #6 and VB 12

C #6 and VB 12 will be added to the pattern match

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.