I have F# class library assembly that contains two functions:
let add a b = a + b
and
let rec aggregateList list init (op:int -> int -> int) =
match list with
|[] -> init
|head::tail ->
let rest = aggregateList tail init op
op rest head
I have a C# console application which references the F# library and is attempting to do the following:
FSharpList<int> l = new FSharpList<int>(1, new FSharpList<int>(2, FSharpList<int>.Empty));
int result = myFsLibrary.aggregateList(l, 0, myFsLibrary.add);
However, the compiler complains that [myFsLibrary.add] cannot be converted from ‘method group’ to FSharpFunc<int, FSharpFunc<int, int>>
You can explicitly create a function using the
FSharpFuncdelegate. In C#, it is more convenient to create function that takes all arguments as a tuple, so you can do that and then convert the function to a curried type usingFuncConvert. Something like:However, if you need to call some F# function from your C# code, it is recommended to expose a function with a C#-friendly interface. In this case, I you can use
Funcdelegate and the first argument should beIEnumerableinstead of F#-specific list type:Then your C# appplication can just use: