c# to f# - Return an FSharpFunc from F# to C# -


i want write function in f#, exposes following type signature c#:

public static fsharpfunc<fsharpfunc<unit,unit>,unit> foo(action<action> f) 

in f# tried writing:

let foo (f : action<action>) : ((unit -> unit) -> unit) = ... 

but produces c# signature:

public static void foo(action<action> f, fsharpfunc<unit,unit> x) 

the f# has treated code equivalently to:

let foo (f : action<action>) (g : unit -> unit) : unit = ... 

of course, these equivalent f#, different in c#. there can produce c# want? (f# 2.0.0.0)

as quick hack, rewrote f# to:

let foo (f : action<action>) ((unit -> unit) -> unit)[] = ... 

then use head in c#.

if write let foo x = fun () -> ... f# compiler optimizes code , compiles method takes 2 arguments (instead of method returning function need). function value result, need "do something" before returning function:

// compiled method taking action, fastfunc , returning void let foo1(x : action<action>) : (unit -> unit) -> unit =    fun f -> f ()  // compiled method taking action , returning fastfunc of fastfunc let foo2(x : action<action>) : ((unit -> unit) -> unit) =    ignore ();   fun f -> f () 

that said, exposing f# function type c# in way bad pattern , shouldn't done. when have f# api supposed used c#, should expose functions delegates, c# consumers can use them naturally (without converting action f# function explicitly). easier write wrapping on f# side.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -