F# powerpack comes with a set of conversion methods to translate from Func<...> to F# functions, either standard or tupled ones. But is it possible to achieve the opposite: in case you want to call from F# code a C# method that takes Func<...> and want to use native F# lambda expression (e.g. fun x -> some_function_of(x))?
If I send a F# function with a signature 'a -> 'b to a C# method that expects Func then F# compiler generates the following error:
This expression was expected to have type Function<'T,'R> but here has type 'T -> 'R
I want to stay with F# lambda expressions but to use a translation layer in order to be able to send them as C# Func lambda. Is this achievable?
F# provides constructors for all delegate types that take F# values of the corresponding function types. E.g. in your case you want to use System.Func<_,_>(fun x -> ...)
which applies the generated constructor of type ('a -> 'b) -> System.Func<'a, 'b>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With