Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call F# function that has a function parameter from C#

Tags:

c#

f#

In F# i have the function:

module ModuleName
let X (y: int -> unit) = ()

How can i call this in C#? Ideally it would look like

ModuleName.X(x => x*x);

But this lambda syntax does not convert implicitly to a FSharpFunc.

like image 995
Paul Nikonowicz Avatar asked Dec 07 '22 15:12

Paul Nikonowicz


1 Answers

The easiest approach would be to expose your API using standard .NET delegate types. In this case, that means that your F# definition should look more like:

let X (y:Action<int>) = ()

However, if you want to keep your F# API the same as it currently is, then you could use FuncConvert.ToFSharpFunc from the C# side:

ModuleName.X(FuncConvert.ToFSharpFunc(x => x*x));
like image 192
kvb Avatar answered Dec 09 '22 04:12

kvb