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