Get started with an easy, short example that showcases the major features of Myra.
This is how you use Myra.
The entry point to the Myra pattern matching machanism is the Match() extension method, that can be invoked from any type of object.
This method takes two parameters:
To create a pattern, use the Pattern.From() method, that takes a Func<T, bool> as a parameter:
var isEven = Pattern<int>.From(n => n % 2 is 0); var isHusky = Pattern<Dog>.From(d => d is Husky);
A normal call to Match can look like this:
number.Match(isEven, n => Console.WriteLine($"{n} is even.")); // You can also inline the pattern number.Match(Pattern<int>.From(n => n % 2 is 0), n => Console.WriteLine($"{n} is even."));
You can match several patterns against the same object.
To try a different pattern with an object, append another call to Match after the first one:
fluffy.Match(Pattern<Dog>.From(d => d is Husky), d => Console.WriteLine($"{d.Name} is truly fluffly.")) .Match(Pattern<Dog>.From(d => d.Heads.Count > 1), d => Console.WriteLine($"{d.Name} is guarding the philosopher's stone."));
All the patterns will be checked, and any matching patterns will be executed. If you need a more advanced condition system, check the Advanced Logic guide.