F#: currying differences between functions and static members

217 views Asked by At

Can please someone explain this to me:

type IItem = interface end
type Item = {i:int} interface IItem

type Fail = static member foo (s:string) = fun (x:IItem) -> ""
let foo = fun (s:string) -> fun (x:IItem) -> ""

let works = {i=1} |> foo ""
let fails = {i=1} |> Fail.foo ""

Why does the currying with the static member function not work? I'm using Visual Studio 2012 with .net 4.5.2 if that matters.

1

There are 1 answers

0
kvb On

This isn't really a difference between static members and functions - it's a bit more subtle. Here's another repro:

type T =
    static member A () (o:obj) = ()
    static member B () = fun (o:obj) -> ()

T.A () 1 // ok
T.B () 1 // huh?

Note that the signatures of T.A and T.B are different (this is actually covered in section 11.2.1.1 of the spec):

type T =
  class
    static member A : unit -> o:obj -> unit
    static member B : unit -> (obj -> unit)
  end

This is a distinction that is usually unimportant, but basically it means that at a .NET representation level A is compiled to a method with two arguments (even though it looks curried in F#) while B is compiled to a method with a single argument which returns an F# function. This difference is ultimately what causes the behavior you're seeing.