Monday, April 25, 2011

multiple parameters to methods in f#

i've got a class written in f# that i'm consuming in c#, that defines a method Render:

member this.Render template (context: IContext) =
    let tokens = Lexer.tokenize template
    let parser = new DefaultParser([for filter in _filters -> filter])

    let resp = new StringBuilder()
    for node in parser.Parse tokens None do
        ignore <| resp.Append(node.render context)

    resp.ToString()

the signature of this method is template:string -> (IContext -> string), which of course reads as "member Render takes a string parameter returns a function that takes an IContext and produces a string

if i change the declaration from "member" to a let binding, defining it as a function local to the class definition:

let Render template (context: IContext) =

then the signature becomes what you would expect it to be - string -> IContext -> string, which reads "Render takes a string, then an IContext and produces a string"

is there a way to make a member behave like the let binding? This is causing issues consumin this member from c#, as the signature becomes Render(string, FastFunc<IContext, string>), which is not overly usable.

tia.

From stackoverflow
  • If you want to expose to C#, you should write it tupled style:

    > type Foo =
    -   member this.Bar (param1, param2) =  param1 + param2;;
    
    type Foo =
      class
        member Bar : param1:int * param2:int -> int
      end
    

    That'll expose a normal .NET style method.

    MichaelGG : Make sure you provide names for the objects in the tuple.
    kolosy : sorry - that was for a slightly different question. your suggestion works for concrete members, but it doesn't work for interfaces defined in f# and then implemented in c#. those come across as Tuple
    Brian : as per http://cs.hubfs.net/forums/thread/9162.aspx lose the extra parens

0 comments:

Post a Comment