How F# can help with the pitfalls of C# enumerations

I don’t get it. Even in a statically typed language like C# you can change the domain model and your application will still compile as if nothing happened. In this post we will address this and see how F# can help with such pitfalls of C# enumerations.

What’s the problem?

The problem with enumerations is simply that when we add new cases especially in a large code base, it can be really hard to find and update all dependent source code which might lead to errors at runtime.

The compiler will not help to find all the places that have to be updated. A text-based search isn’t reliable.

Here is how things can go wrong.

Continue reading →

Convert an Action into an F# function returning unit

Let’s say we want to pass an Action<T> as an argument from C# code to a F# function that might look something like this:

module MyModule = 

    let doSomething log =
        do log "doing something"
        "result"

It took me an almost unreasonable amount of time today to figure out how to do this. So that’s why I’d like to share this.

First since F# doesn’t know actions the action has to be converted to a Func<T, Unit>. The tricky part is to create an instance of Unit because Unit neither has a public constructor nor any factory methods. After referencing Microsoft.FSharp.Core it can be done with reflection:

var unit = (Unit)Activator.CreateInstance(typeof(Unit), true)

Now we convert the Action<T> to a Func<T, Unit>:

public static Func<T, Unit> ToFunc<T>(this Action<T> action)
{
    return x => { action(x); return (Unit)Activator.CreateInstance(typeof(Unit), true); };
}

Next the new function can be converted to an FSharpFunc<T, Unit>:

public static FSharpFunc<T, Unit> ToFSharpFunc<T>(this Action<T> action)
{
    return FSharpFunc<T, Unit>.FromConverter(new Converter<T, Unit>(action.ToFunc()));
}

Here is the complete code that does everything:

using System;
using Microsoft.FSharp.Core;

namespace FSharpConverter
{
    public static class ToFSharpFuncConverterExtensions
    {
        private static readonly Unit Unit = (Unit)Activator.CreateInstance(typeof(Unit), true);

        public static Func<T, Unit> ToFunc<T>(this Action<T> action)
        {
            return x => { action(x); return Unit; };
        }

        public static FSharpFunc<T, Unit> ToFSharpFunc<T>(this Action<T> action)
        {
            return FSharpFunc<T, Unit>.FromConverter(new Converter<T, Unit>(action.ToFunc()));
        }
    }
}

Now we can use this in our C# code:

Action<string> logger = Console.WriteLine;
var fsharpLogger = logger.ToFSharpFunc();
MyModule.doSomething(fsharpLogger);

Example

Here is an example where I used this: GUI for a functional Tic-Tac-Toe implementation

Update

In the meantime I found out that there is actually a very rich library that supports this and a lot more C# – F# interoperability: FSharpx.Extras