Last active
August 29, 2015 14:22
-
-
Save battermann/59af51dad9d798d7c9d8 to your computer and use it in GitHub Desktop.
How to pass an argument to a F# function that takes ('a -> unit) from C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Action<string> logger = Console.WriteLine; | |
var fsharpLogger = logger.ToFSharpFunc(); | |
MyModule.doSomething(fsharpLogger); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment