问题
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.
回答1:
Try:
let g = f.Invoke |> FuncConvert.FuncFromTupled
回答2:
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.
来源:https://stackoverflow.com/questions/19290393/calling-c-sharp-function-with-multiple-arguments-from-f