Functional programming in C#: What is missing?

The last years I have been developing business applications in C#. However if you would ask me, “what is your favorite programming language” I would still tell you Haskell. I really love functional programming. However if a customer, I would like an app with a nice user interface I would not use Haskell (or another functional programming language). User interfaces are really troublesome to create in a functional programming language. Because almost all applications that I create have some sort of user interface I never use functional programming languages. Of course we could use multiple programming languages to create a single application, create the formula’s in a functional programming language and the user interface in for instance C#, however I do not think this is the right answer. I rather program in one single language what makes it easier to work in teams, all team members need to know/learn one language and all team members can read and understand all code that is written for the application.

The last years however .NET is evolving more and more to a functional programming language. In .NET framework we got generics what makes functional programming possible. In the .NET 3.0 framework we got Linq, an framework of extension methods that give kind of a functional feeling to programmers. Furthermore Linq gave us the possibility to compile check and validate queries that we use for database what is really a big feature if you ask me. Furthermore lambda methods where introduced what reduced the need to implement small methods that really did not have any significance in an object oriented way of developing. Also basic typeinferencing was introduced what made functional programming better feasible.

Some people now recon that C# is an hybrid between functional and object oriented programming and I really love the idée. One language that we can use for both functional and object oriented programming would really be lovely. Especially when we get multi core processors (with at least 10 cores) I think that we really need a functional approach because locking in an object oriented application will really be a bottle neck and introduces a lot of complexities that will reduce the lifetime of software. So I really think that we need functional programming in .NET framework.

However in C# 4.0 I am still missing some basic functional features.

Function composition
One of the most import bases of functional programming is function composition. If a developer thinks functional; he or she only knows functions. For instance an statement like

int a = 6;

is not a variable assignement for a functional programmer, it is a definition of a function that will always return the value 6. Within a functional language you only define functions and you compose functions by concatenating other functions. This principle is called function composition. Within a functional language if we have a function like

public static string TestArg2(string arg1, string arg2)

we would like to compose a function in the following way

Func = TestArg2(“first argument”);

We give the value for the first argument however the second argument we will fill later. Currently this does not work in C# 4.0. It will give an compile error like “No overload for method TestArg2 takes 1 argument”. In a functional way you would rather like it to return a function that still expects the second argument. It should however throw an exception if you would type

Int result = TestArg2(“first argument”);

Because Func cannot be cast to an int. This can be done by type inferencing so it would be a compile error.

The current solution to this problem is the following code.

Func res = c => TestArg2("test",c);

However it looks much more complex and less readable if you ask me.

Type inferencing should be much better

When programming c# I for instance come across a lot of functions like

public static T GenericMethod<T>()
where T : new()
{
return new T();
}

If I need to call a function like this I would expect something like

int result = GenericMethod();

Type inferencing should see that the return type is int; so T is of type int. In c# however you need to explicitly specify it, only the following method call compiles.

int result = GenericMethod<int>();

Type inferencing is very important for functional programming, it is however minimal implemented in .NET framework 4.0.

Probably I will add more points that are missing in a later blog.



Comments

Popular posts from this blog

System.Net.Http dll version problems

SharePoint Survey Back Button

Azure table storage error: One of the request inputs is not valid