V.B.
V.B.

Reputation: 6382

Calling C# function with multiple arguments from F#

It is easy to call f:Func<'T, 'T> from F# as 'T -> 'T by using f.Invoke

But how should I call f:Func<'T, 'T, 'T> from F# as 'T -> 'T -> 'T?

When I use f.Invoke I get 'T * 'T -> 'T, i.e. a tuple instead of two arguments.

Upvotes: 5

Views: 496

Answers (2)

Tomas Petricek
Tomas Petricek

Reputation: 243041

Note that you do not need to turn the function into a function taking two arguments if you just want to call it.
If you already know the arguments for the function, then you can just call it as a function taking a tuple:

f.Invoke(40, 2)

Converting the Func<T1, T2, R> delegate to a function 'T1 -> 'T2 -> 'R is still useful though, if you need to pass it to some code that expects a curried function (multiple-argument function) of this type. But even then, you can write that using an explicit lambda:

functionTakingTwoArgumentFunc (fun a b -> f.Invoke(a, b))

... and this is exactly what FuncConvert.FuncFromTupled does.

Upvotes: 8

kwingho
kwingho

Reputation: 422

Try:

let g = f.Invoke |> FuncConvert.FuncFromTupled

Upvotes: 9

Related Questions