开发者

Interop between F# and C# lambdas

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>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜