What are the limitations of pattern matching mechanics?

Personally, I only know that dynamics cannot be used in comparison with a sample, which is considered pathetic :(

dynamic foo = 10; switch(foo) { case int i: break; } 

In addition, valuable tuples / neo-tuples cannot be used when matching patterns:

 dynamic foo = (420, 360); switch(foo) { case (int, int) i: break; } 

It has been removed in the current version of C # 7 and has been designated for future use.

What other things can I do?

+5
source share
1 answer

The new pattern matching features in C # 7 consist of the following:

  • Support switch types,
  • Simple use of var patterns,
  • Adding when guards to case statements,
  • pattern expression x is T y .

Your examples focus on the first one. And the switch type is likely to be the most popular and is often used in these new features. Although there are limitations, such as the ones you mentioned, other functions can be used to work in many of them.

For example, your first restriction is easily resolved by boxing foo to object :

 dynamic foo = 10; switch ((object)foo) { case int i: Console.WriteLine("int"); break; default: Console.WriteLine("other"); break; } 

Print int as expected.

The var pattern and guard can be used to circumvent the second limitation:

 dynamic foo = (420, 360); switch (foo) { case var ii when ii.GetType() == typeof((int, int)): Console.WriteLine("(int,int)"); break; default: Console.WriteLine("other"); break; } 

will print (int,int) .

Alternatively, value tuples can be used to switch types, you just need to use the long syntax:

 var foo = (420, 360); switch (foo) { case ValueTuple<int,int> x: Console.WriteLine($"({x.Item1},{x.Item2})"); break; default: Console.WriteLine("other"); break; } 

The above text will print (420,360) .

For me personally, the biggest limitation on pattern matching in C # 7 is the lack of expressions matching the pattern using the match keyword. Initially, the following was planned for this version:

This can be circumvented using switch , but it is randomly:

 var x = 1; var y = IntOrOther(x); ... private string IntOrOther(int i) { switch (i) { case int _ : return "int"; default: return "other"; } } 

But there is help with many third-party pattern matching libraries, for example, my own, which allows you to write it as:

 var x = 1; var y = x.TypeMatch().To<string>() .Caseof<int>().Do("int") .Else("other") .Result(); 

This is not as good as having the match keyword, but it is an optional workaround until this feature appears in a later release of the language.

To understand the limitations imposed by C # 7, it’s worth looking at the pattern matching specification and comparing it with what will be in the next C # release. However, looking at him, it becomes obvious that everything is working out for them.

This question was originally closed because it was open, as currently formulated. To give a couple of silly examples, the limitations on matching the C # 7 template are that it won’t make you the perfect cup of coffee or it can fly around the world in seconds ... but I prefer to answer the spirit of the question. And the answer really is that the only limitation is your imagination. If you do not allow this to limit you, then you must consider the fact that workarounds have readability and / or performance implications. Most likely, these are the only restrictions in the real world.

+7
source

Source: https://habr.com/ru/post/1263766/


All Articles