You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/async.html
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -120,7 +120,7 @@ <h2 id="async-match-expressions">Async Match Expressions</h2>
120
120
pattern is matched successfully (sync actions are turned into async actions).</p>
121
121
<p>Async match expressions can be executed using the <code>ExecuteAsync</code>, <code>ExecuteNonStrictAsync</code> and
122
122
<code>ExecuteWithFallthroughAsync</code> methods. The <code>ToFunction</code> method and its variations are also available.
123
-
<code>ExecuteWithFallthroughAsync</code> returns an <code>IAsyncEnumerable</code> which enable lazy async execution of match expressions.</p>
123
+
<code>ExecuteWithFallthroughAsync</code> returns an <code>IAsyncEnumerable</code> which enables lazy async execution of match expressions.</p>
124
124
<p>The <code>Matchmaker.Linq</code> namespace contains the <code>EnumerateAsync</code> extension method for <code>IAsyncEnumerable<T></code> which
125
125
enumerates it and ignores the result. You can use it if you just want to execute the match statement with fall-through.</p>
126
126
<p>Static async match expressions are also available. Use the <code>AsyncMatch.CreateStatic</code> methods to create them, just like
Copy file name to clipboardExpand all lines: articles/expressions.html
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -95,9 +95,9 @@ <h3 id="creating-match-expressions">Creating Match Expressions</h3>
95
95
<p>A match expression can be created using the <code>Create</code> methods of the static class <code>Match</code>.</p>
96
96
<h3id="adding-cases">Adding Cases</h3>
97
97
<p>The <code>Match</code> classes include <code>Case</code> methods which are used to add a pattern and a function which is executed if the match
98
-
is successful. Match expressions are immutable –<code>Case</code> methods return new match expressions; they do not affect the
98
+
is successful. Match expressions are immutable —<code>Case</code> methods return new match expressions; they do not affect the
99
99
ones on which they are called.</p>
100
-
<p><code>Case</code> methods are generic – they also contain information about the pattern's transformation type. Match expressions
100
+
<p><code>Case</code> methods are generic — they also contain information about the pattern's transformation type. Match expressions
101
101
can contain patterns of arbitrary transformation types without knowing about these types.</p>
102
102
<h3id="executing-match-expressions">Executing Match Expressions</h3>
103
103
<p>To execute a match expression, the <code>ExecuteOn</code> method is used. It takes the input value to match. There are two modes of
@@ -108,7 +108,7 @@ <h3 id="executing-match-expressions">Executing Match Expressions</h3>
108
108
which executes the match expression in the non-strict mode. It returns <code>MatchResult<TOutput></code> because the result might
109
109
not be present.</p>
110
110
<p>In the <code>Match<TInput></code> class, the <code>ExecuteOn</code> method doesn't return anything, and also throws a <code>MatchException</code> if the
111
-
match wasn't successful. This class also contains the <code>ExecuteNonStrict</code> method – it returns a boolean value which
111
+
match wasn't successful. This class also contains the <code>ExecuteNonStrict</code> method — it returns a boolean value which
112
112
indicates whether the match was successful and doesn't throw an exception if it wasn't.</p>
113
113
<p>The <code>ToFunction</code> method and its variations are also available. They return a function which, when called, will execute
114
114
the match expression.</p>
@@ -123,8 +123,8 @@ <h2 id="matching-with-fall-through">Matching with Fall-through</h2>
123
123
is enabled for a pattern, then the expression will continue searching for the next successful pattern. If it isn't, then
124
124
the expression will stop at this pattern and not go any further.</p>
125
125
<p><code>Match.Create</code> is also overloaded to take the default fall-through behavior.</p>
126
-
<p>Matching with fall-through is lazy i.e. it returns an <code>IEnumerable</code> and is only executed when this enumerable is
127
-
enumerated. Because matching will fall-through is lazy, it doesn't have any modes of execution – the user must decide
126
+
<p>Matching with fall-through is lazy, i.e., it returns an <code>IEnumerable</code> and is only executed when this enumerable is
127
+
enumerated. Because matching will fall-through is lazy, it doesn't have any modes of execution — the user must decide
128
128
whether to throw an exception or not if there were no successful matches.</p>
129
129
<p>In the <code>Match<TInput, TOutput></code> class, the <code>ExecuteWithFallthrough</code> method returns an <code>IEnumerable<TOutput></code> which can
only once, and its initialization code is in the same place as its execution point.</p>
200
200
<p>The parameter of the build action has the type <code>MatchBuilder<TInput, TOutput</code> or <code>MatchBuilder<TInput></code>, depending on
201
201
which type of match expressions you are building. This type has the same methods for adding cases as the <code>Match</code> classes
202
-
and is mutable – the methods return the same builder instance.</p>
202
+
and is mutable — the methods return the same builder instance.</p>
203
203
<p><code>MatchBuilder</code> also has the <code>Fallthrough</code> method which specifies the default fall-through behavior. But this method
204
204
specifies fall-through behavior only for cases that are defined after it. For example:</p>
<p>If you need to migrate from older versions, first migrate to version 1.2.0. You can do that just by reading the
93
93
changelog, because previous versions contained much fewer changes and it's much simpler to migrate.</p>
94
-
<p>In order to migrate from version 2.0.0 to version 2.1.0 action is required only if you implemented the
94
+
<p>In order to migrate from version 2.0.0 to version 2.1.0, action is required only if you implemented the
95
95
<code>IPattern<TInput, TMatchResult></code> interface (which wasn't recommended). The less generic <code>IPattern<TInput></code> interface was
96
96
removed, so you should remove its <code>Match</code> method. That's it.</p>
97
97
<p>No changes are required to migrate from version 2.1.0 to 3.1.0. Version 3.0.0 removed support for .NET Standard 2.0, but
98
-
version 3.1.0 added it back, effectively undoing the only breaking change of 3.0.0.</p>
98
+
version 3.1.0 added it back, effectively undoing its only breaking change.</p>
99
99
<h2id="general">General</h2>
100
100
<p>Since this library has a new name, the namespace is different as well. Instead of the <code>PatternMatching</code> namespace there
101
101
are three namespaces: <code>Matchmaker</code>, <code>Matchmaker.Patterns</code> and <code>Matchmaker.Linq</code>.</p>
<li>Matches the input value with the pattern and returns a successful result if the match is successful.</li>
102
102
<li>Transforms the input value. The returned result contains the transformed value if it's successful.</li>
103
103
</ul>
104
-
<p>Since options are not supported natively in C#, a custom type –<ahref="results.html"><code>MatchResult<T></code></a>– is used.</p>
104
+
<p>Since options are not supported natively in C#, a custom type —<ahref="results.html"><code>MatchResult<T></code></a>— is used.</p>
105
105
<p>The definition of patterns is similar to F#'s active patterns.</p>
106
-
<p>Descriptions for patterns are not terribly important, but can be useful for debugging. As such, they are optional – if
106
+
<p>Descriptions for patterns are not terribly important, but can be useful for debugging. As such, they are optional — if
107
107
you don't want a pattern to have a description, it should be empty. A pattern's description should never be <code>null</code>.</p>
108
108
<h2id="null-values">Null Values</h2>
109
109
<p>The results of patterns' matching can be <code>null</code>. This is why the <code>MatchResult<T></code> type can contain a <code>null</code> value.</p>
@@ -131,7 +131,7 @@ <h2 id="linq-to-patterns">LINQ to Patterns</h2>
131
131
<p>The <code>Matchmaker.Linq</code> namespace provides several extension methods for patterns:</p>
132
132
<ul>
133
133
<li><code>Select</code> maps a pattern's result value if it's successful.</li>
134
-
<li><code>Pipe</code> creates a pattern pipeline – the result of the first pattern is the input of the second pattern.</li>
134
+
<li><code>Pipe</code> creates a pattern pipeline — the result of the first pattern is the input of the second pattern.</li>
135
135
<li><code>Cast</code> casts a pattern's result to a specified type. It's the same as piping a pattern to the <code>Type</code> pattern. If the
136
136
input is <code>null</code>, then the match will fail only if the destination type is a non-nullable value type.</li>
137
137
<li><code>Bind</code> flat-maps a pattern's result. If a pattern's result is successful, it calls the specified function and passes
@@ -142,17 +142,17 @@ <h2 id="linq-to-patterns">LINQ to Patterns</h2>
142
142
the input if successful.</li>
143
143
<li><code>Compose</code> is the same as the three methods above, but the composition operator is passed to it as well.</li>
144
144
<li><code>Cached</code> returns a pattern which matches the same as the specified pattern but caches its results in a <code>null</code>-safe
145
-
hash table. Every input will be matched only once – if it's matched again, the result will be taken from the cache. The
145
+
hash table. Every input will be matched only once — if it's matched again, the result will be taken from the cache. The
146
146
caching process is not thread-safe.</li>
147
147
</ul>
148
148
<p>All extension methods for patterns are overloaded to take a custom description.</p>
149
149
<p>Since there are the <code>Select</code> and <code>Where</code> extensions on patterns, you can write them using C#'s query syntax.</p>
150
150
<p>Patterns have the <code>Bind</code> and <code>Return</code> functions, so they are monads. To be more specific, patterns can be thought of as
151
151
a combination of the Reader and Maybe monads.</p>
152
152
<h2id="immutability">Immutability</h2>
153
-
<p>Every predefined pattern as well as patterns returned by the <code>CreatePattern</code> and extension methods are immutable.
154
-
Calling an extension method on a pattern returns a new pattern – the old one is unchanged.</p>
155
-
<p>An exception is the pattern returned by the <code>Cached</code> method, which is not immutable – it holds a mutable cache. But if a
153
+
<p>All predefined patterns, as well as patterns returned by <code>CreatePattern</code> and extension methods, are immutable. Calling
154
+
an extension method on a pattern returns a new pattern — the old one is unchanged.</p>
155
+
<p>An exception is the pattern returned by the <code>Cached</code> method, which is not immutable — it holds a mutable cache. But if a
156
156
pattern is referentially transparent (its <code>Match</code> method always returns the same result for the same input and doesn't
157
157
have any side effects), then the caching pattern based on it can be thought of as immutable as well, because it doesn't
158
158
matter how many times the base pattern's <code>Match</code> method is called.</p>
<p>As you can see, we have to throw an exception in the <code>switch</code> version, because C# can't know that <code>ConsCell</code> and <code>Empty</code>
150
150
are the only possible subclasses of <code>ConsList</code>. And for that reason, if we forget to define one of the cases in <code>switch</code>
151
-
or in a match, we'll get an exception. In F# a warning is issued when the match is incomplete, but C# doesn't have the
151
+
or in a match, we'll get an exception. In F#, a warning is issued when the match is incomplete, but C# doesn't have the
152
152
notion of complete or incomplete matches. Of course, this match will fail if the provided list is <code>null</code>, but this can
153
153
be handled using the <code>Null</code> pattern.</p>
154
154
<p>With C# 8, there's a better way to match on discriminated unions, but we still have to explicitly throw an exception in
0 commit comments