Looking at the set of features introduced in C# 7, I am thinking –

Is C# trying to follow the footsteps of Scala (a functional language that compiles into JVM bytecode)?

To answer the question let’s look at the features that have been introduced.

I got a chance to work with Scala while working at agoda (It was my first introduction to functional programming and I found it quite natural and more mathematical). Scala is a great programming language and has very concise and expressive syntax. Now coming back to C# and the features that have been introduced. We will take them one at a time so let’s start with something that’s my favorite.

  1. Embedded/Local functions: Okay so what is a functional language? well a functional language is a language where functions can be treated like data – what this means is that you can:
    1. Pass a function as a parameter to another function (already supported through lambdas in C# since version 3.0).
    2. Return a function as output from a function (Already supported through delegates since or before version 3.0 – using Action<T> and Func<T>).
    3. Declare and instantiate a function inside another function like any other variable: Think about the situations where you needed to create a separate function (for the sake of readability or may be for recursion) so that you can use it inside just one function.

C# 7.0 allows you to do something like this:

Function1

This was already available in functional languages like JavaScript and Scala but was missing from C#, you could however do something like this (and the above code is just a syntactic sugar for the below:

Function2

OR

Function3

Now depending on your taste and preferences (and the human tendency to stay in comfort) you might find the below two examples more understandable but if you look at the fact that the function Fib is not being used anywhere else besides being called from the Fibonacci function, the existence of the Fib function in the class (even if it’s private) is not justified.

The same can be rewritten as (and this is as short as it can be):

Function4

Now you might say that the above is more concise (in terms of number of lines of code) and may be more understandable but still the Fib function can never be defined inside the Fibonacci function up until now, if you try to do something like this (before C# 7.0) you would get big red lines of horror:

Function5

You must have figured out by now that recursion was not supported in lambdas and hence the squiggly below Fib call, now the below function is perfectly valid in C# (because it’s a variable and not a function):

Function6

Here the value “i” can reference itself in the add (+) operator call (binary operators are also functions with LHS and RHS as input parameters and return an output). So C# was treating data and functions differently.

2. Deconstruction: We have all seen constructors and they have been around since ages. Deconstructors are a mechanism for breaking down the complex types as combination of simple or other complex types. “Deconstruct” is similar to “unapply” method of Scala. Deconstruction is especially helpful in pattern matching (another great feature of Scala and now C# as well – saving it for another article). Deconstruction allows you to do something like:

Function7

So you can create methods that can reverse the construction process and extract values (simple or complex) out of objects. Without this you would have to do something like this:

Function8

Or if your “Point” class code is inaccessible and it is sealed then:

Function9

You can make it even more concise by doing something like:

Function10

This is quite concise and subtle but this still doesn’t beat the simplicity of the new deconstruction feature. Moreover you can add discards like this:

Function11

So the value of “Y” would simply be discarded.

We will discuss more of these in the next article.

Advertisements