1- ## Match Expressions
1+ # Match Expressions
22
33The second central idea is the match expression itself. It is represented by two classes: ` Match<TInput, TOutput> `
44and ` Match<TInput> ` . The difference between them is that the former represents a match expression which yields
@@ -13,7 +13,7 @@ A match expression can be created using the `Create` methods of the static class
1313### Adding Cases
1414
1515The ` Match ` classes include ` Case ` methods which are used to add a pattern and a function which is executed if
16- the match is successful. Match expressions are immutable - ` Case ` methods return new match expressions, they
16+ the match is successful. Match expressions are immutable - ` Case ` methods return new match expressions; they
1717do not affect the ones on which they are called.
1818
1919` Case ` methods are generic - they also contain information about the pattern's transformation type. Match expressions
@@ -24,23 +24,24 @@ expressions.
2424### Executing Match Expressions
2525
2626To execute a match expression, the ` ExecuteOn ` method is used. It takes the input value to match. There are two modes
27- of execution in match expressions: strict and non-strict. The strict mode throws an exception and the non-strict doesn't.
27+ of execution in match expressions: strict and non-strict. The strict mode throws an exception if no matches were found,
28+ and the non-strict doesn't.
2829
29- In the ` Match<TInput, TOutput> ` class the ` ExecuteOn ` method returns the result of the match, or throws a
30+ In the ` Match<TInput, TOutput> ` class the ` ExecuteOn ` method returns the result of the match or throws a
3031` MatchException ` if no successful match was found. ` Match<TInput, TOutput> ` also contains the ` ExecuteNonStrict `
31- method which executes the match expression in the non-strict mode. It returns ` MatchResult<TOutput> ` , because
32+ method which executes the match expression in the non-strict mode. It returns ` MatchResult<TOutput> ` because
3233the result might not be present.
3334
34- In the ` Match<TInput> ` class the ` ExecuteOn ` method doesn't return anyhting , and also throws the ` MatchException `
35+ In the ` Match<TInput> ` class the ` ExecuteOn ` method doesn't return anything , and also throws a ` MatchException `
3536if the match wasn't successful. This class also contains the ` ExecuteNonStrict ` method - it returns a boolean value
36- which indicates whether the match was successful, and doesn't throw an exception if it wasn't.
37+ which indicates whether the match was successful and doesn't throw an exception if it wasn't.
3738
3839The ` ToFunction ` method and its variations are also available. They return a function which, when called, will execute
3940the match expression.
4041
4142## Matching with Fall-through
4243
43- C, C++ and Java support fall-through in ` switch ` expressions . So does this library, although it works differently here.
44+ C, C++ and, Java support fall-through in ` switch ` statements . So does this library, although it works differently here.
4445
4546Fall-through must be explicitly enabled for cases and then explicitly enabled during execution. Both ` Match ` classes
4647contain the ` ExecuteWithFallthrough ` method which takes fall-through behavior into account. ` ExecuteOn ` and
@@ -49,16 +50,16 @@ contain the `ExecuteWithFallthrough` method which takes fall-through behavior in
4950If a case has fall-through enabled, then the expression falls to the _ next successful_ match, unlike ` switch ` , which
5051falls to the next case whether it's successful or not.
5152
52- Matching with fall-through is lazy i.e. it returns an ` IEnumerable ` and is only executed when this enumerable
53- is enumerated. Because matching will fall-through is lazy, it doesn't have any modes of execution - the user must
54- decide whether to throw exceptions or not if there were no successful matches.
55-
5653The ` Case ` methods are overloaded to accept a boolean value which indicates the fall-through behavior. If fall-through
5754is enabled for a pattern, then the expression will continue searching for the next successful pattern. If it isn't, then
5855the expression will stop at this pattern and not go any further.
5956
6057` Match.Create ` is also overloaded to take the default fall-through behavior.
6158
59+ Matching with fall-through is lazy i.e. it returns an ` IEnumerable ` and is only executed when this enumerable
60+ is enumerated. Because matching will fall-through is lazy, it doesn't have any modes of execution - the user must
61+ decide whether to throw an exception or not if there were no successful matches.
62+
6263In the ` Match<TInput, TOutput> ` class the ` ExecuteWithFallthrough ` method returns an ` IEnumerable<TOutput> `
6364which can be used to get all successful match results.
6465
@@ -76,7 +77,7 @@ it and ignores the result. You can use it if you just want to execute the match
7677
7778** Remember: matching with fall-through is lazy and is actually executed when the result is enumerated.**
7879
79- Here's a (somewhat convulted ) implementation of the famous fizz-buzz program which uses matching with fall-through:
80+ Here's a (somewhat convoluted ) implementation of the famous fizz-buzz program which uses matching with fall-through:
8081
8182```
8283using System.Linq;
@@ -107,8 +108,8 @@ var result = Enumerable.Range(0, 15)
107108
108109### The Initialization Problem
109110
110- One pain point of match expressions is that wherever a method which contains a match expression is executed, the match
111- expression is initialized from scratch. Take a look at the example:
111+ One pain point of match expressions is that whenever a method which contains a match expression is executed, the match
112+ expression is initialized from scratch. Take a look at this example:
112113
113114```
114115void DoStuff(int i)
@@ -125,14 +126,14 @@ as well, even though it's actually the same expression. Having just 4 cases may
125126accumulate if we execute it thousands of times.
126127
127128We can save the expression in a field and then call the ` ExecuteOn ` method on this field. But this makes the code
128- much less readable, because the case definitions are in a different place from the actual execution point.
129+ much less readable because the case definitions are in a different place from the actual execution point.
129130
130131### The Solution
131132
132133There is a way to create 'static match expressions' - expressions which will be initialized only once.
133134
134- The ` Match ` class contains ` CreateStatic ` methods which allow the creation of static match expressions. Take a look
135- at the modified example.
135+ The ` Match ` class contains the ` CreateStatic ` methods which allow the creation of static match expressions.
136+ Take a look at the modified example:
136137
137138```
138139void DoStuff(int i)
@@ -144,9 +145,9 @@ void DoStuff(int i)
144145 .ExecuteOn(i);
145146```
146147
147- It looks almost the same, except for one difference: he calls to ` Case ` methods are inside the lambda expression,
148- called the build action, which is passed to the ` CreateStatic ` method. Now this match expressions will be initialized
149- only once and its initialization code is in the same place as its execution point.
148+ It looks almost the same, except for one difference: the calls to ` Case ` methods are inside the lambda expression,
149+ called the build action, which is passed to the ` CreateStatic ` method. Now this match expression will be initialized
150+ only once, and its initialization code is in the same place as its execution point.
150151
151152The parameter of the build action has the type ` MatchBuilder<TInput, TOutput ` or ` MatchBuilder<TInput> `
152153depending on which type of match expressions you are building. This type has the same methods for adding cases as
0 commit comments