Dedicated match method
domn1995 opened this issue · 1 comments
domn1995 commented
As a developer, I want a dedicated Match()
method, so that consumers of my union type are required to handle each case in order to get a value out of it.
Usage might look something like this:
[Union]
public interface IShape
{
IShape Circle(double radius);
IShape Rectangle(double length, double width);
IShape Triangle(double @base, double height);
}
var shape = new Rectangle(3, 4);
var area = shape.Match(
circle => 3.14 * circle.Radius * circle.Radius,
rectangle => rectangle.Length * rectangle.Width,
triangle => triangle.Base * triangle.Height / 2
);
Console.WriteLine(area); // "12"