55A library which enables more powerful pattern matching than is currently available in the C#'s ` switch `
66statement/expression.
77
8- This library is a successor of [ PatternMatching] ( https://github.com/TolikPylypchuk/PatternMatching ) .
9- Version 1.x can be found there. This repository contains version 2+.
8+ This library is a successor of [ PatternMatching] ( https://github.com/TolikPylypchuk/PatternMatching ) . Version 1.x can be
9+ found there. This repository contains version 2+.
1010
1111## Installation
1212
@@ -65,13 +65,12 @@ While this example doesn't show the full power of pattern matching, there are a
6565
6666- The match expression yields a result. We don't have to assign the result explicitly in each case.
6767
68- - The input of the match expression is specified _ after_ all the cases. This allows us to save the match expression
69- in an object, and use it multiple times on different input values.
68+ - The input of the match expression is specified _ after_ all the cases. This allows us to save the match expression in
69+ an object, and use it multiple times on different input values.
7070
7171- The default case is a pattern, just like any other. It's called ` Any ` and is always matched successfully.
7272
73- - Like in ` switch ` the patterns are tried out sequentially. This means that the ` Any ` pattern should always
74- come last.
73+ - Like in ` switch ` the patterns are tried out sequentially. This means that the ` Any ` pattern should always come last.
7574
7675C# 8 included a new way to write ` switch ` expressions which yield a value, and further versions extended it quite a bit.
7776This drastically reduced the need for external libraries like this one for pattern matching. However, this library lets
@@ -92,13 +91,13 @@ string result = i switch
9291};
9392```
9493
95- OK, this is much shorter and cleaner than the previous two examples. But this library shines when the patterns are
96- more complex. While C# allows various kinds of patterns, this library allows anything you can think of.
94+ OK, this is much shorter and cleaner than the previous two examples. But this library shines when the patterns are more
95+ complex. While C# allows various kinds of patterns, this library allows anything you can think of.
9796
9897## Another Example
9998
100- Let's define a simple list, implemented as [ cons cells] ( https://en.wikipedia.org/wiki/Cons ) . This list is not
101- generic for simplicity.
99+ Let's define a simple list, implemented as [ cons cells] ( https://en.wikipedia.org/wiki/Cons ) . This list is not generic
100+ for simplicity.
102101
103102``` c#
104103public abstract class ConsList
@@ -132,8 +131,8 @@ public sealed class Empty : ConsList
132131}
133132```
134133
135- Now let's look what pattern matching on the list would look like. Let's create
136- a function which finds the sum of all items of the list.
134+ Now let's look what pattern matching on the list would look like. Let's create a function which finds the sum of all
135+ items of the list.
137136
138137``` c#
139138public int Sum (ConsList list ) =>
@@ -162,13 +161,13 @@ public int Sum(ConsList list)
162161}
163162```
164163
165- As you can see, we have to throw an exception in the ` switch ` version, because C# can't know that ` ConsCell `
166- and ` Empty ` are the only possible subclasses of ` ConsList ` . And for that reason, if we forget to define one
167- of the cases in ` switch ` or in a match, we'll get an exception. In F#, a warning is issued when the match is
168- incomplete, but C# doesn't have the notion of complete or incomplete matches.
164+ As you can see, we have to throw an exception in the ` switch ` version, because C# can't know that ` ConsCell ` and ` Empty `
165+ are the only possible subclasses of ` ConsList ` . And for that reason, if we forget to define one of the cases in ` switch `
166+ 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
167+ notion of complete or incomplete matches.
169168
170- With C# 8 there's a better way to do this, but we still have to explicitly throw an exception
171- in the default case (which we know won't happen):
169+ With C# 8 there's a better way to do this, but we still have to explicitly throw an exception in the default case (which
170+ we know won't happen):
172171
173172``` c#
174173public int Sum (ConsList list ) =>
@@ -217,8 +216,8 @@ var result = Enumerable.Range(0, 15)
217216## Static Match Expressions
218217
219218One pain point of match expressions is that whenever a method which contains a match expression is executed, the match
220- expression is initialized from scratch every time. This can be solved with static match expressions. Take a look at
221- the revised simple example:
219+ expression is initialized from scratch every time. This can be solved with static match expressions. Take a look at the
220+ revised simple example:
222221
223222``` c#
224223string result = Match .CreateStatic <int , string >(match => match
@@ -230,29 +229,28 @@ string result = Match.CreateStatic<int, string>(match => match
230229 .ExecuteOn (5 );
231230```
232231
233- Now this match expression will be initialized only once even if its containing method is executed multiple times.
234- You can read more [ here] ( https://matchmaker.tolik.io/articles/expressions.html#static-match-expressions ) .
232+ Now this match expression will be initialized only once even if its containing method is executed multiple times. You
233+ can read more [ here] ( https://matchmaker.tolik.io/articles/expressions.html#static-match-expressions ) .
235234
236235## More Info
237236
238- If you want to learn how to use this library, you should read the [ documentation] ( https://matchmaker.tolik.io ) .
239- The articles provide everything you need to know to use this library.
237+ If you want to learn how to use this library, you should read the [ documentation] ( https://matchmaker.tolik.io ) . The
238+ articles provide everything you need to know to use this library.
240239
241240If you need extensive information, go to the [ API reference] ( https://matchmaker.tolik.io/api/index.html ) .
242241
243242If you need even more info about this library, you can go through the
244- [ tests] ( https://github.com/TolikPylypchuk/Matchmaker/Matchmaker.Tests ) . They are property-based and as such
245- they describe every aspect of the classes and their members.
243+ [ tests] ( https://github.com/TolikPylypchuk/Matchmaker/Matchmaker.Tests ) . They are property-based and as such, they
244+ describe every aspect of the classes and their members.
246245
247246## Is This Library Still Maintained?
248247
249- I'm not planning on writing new versions beyond 3.1 (or maybe 3.2 if some stuff needs fixing). To be fair, I thought
250- the same thing after releasing version 1.1 and yet here we are. This time I do believe that this library has enough
251- features (probably more than enough). Maybe one day I'll revisit this decision, but for now (June 2025) this is it;
252- this is as good as it gets.
248+ I'm not planning on writing new versions beyond 3.1. To be fair, I thought the same thing after releasing version 1.1
249+ and yet here we are. This time I do believe that this library has enough features (probably more than enough). Maybe one
250+ day I'll revisit this decision, but for now (June 2025) this is it; this is as good as it gets.
253251
254- That said, if you report a bug or request a new feature, I'll definitely look into it. I'm not giving up on this
255- library any time soon.
252+ That said, if you report a bug or request a new feature, I'll definitely look into it. I'm not giving up on this library
253+ any time soon.
256254
257255## Icon
258256
0 commit comments