Simulating Algebraic Data Types (AKA tagged unions) and pattern matching them in C#

After programming for a bit in Scala I’ve fallen in love with functional programming. And given how concise the code is there’s not much to hate in it.

One of the fine features of Scala is an easy way of defining ADT’s.

Here’s a comparision of defining same ADT with C# and Scala:

Scala is a lot more concise, but that’s just the way C# is – noisy.

Now lets take a look on traditional approach of matching those.

Scala:

C#:

Good thing we can at least use some custom code and functional magic to make that similar to our C# version.

Given that we use this:

We can transform C# code into following:

Which isn’t perfect but is a whole lot nicer.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s