MiloDC
MiloDC

Reputation: 2415

Why can I pass an F# function to a System.Func parameter only in a member method?

This works:

type MyType () =
    static member MyFn (fn : Func<bool>) = fn.Invoke ()

MyType.MyFn (fun _ -> false)

This does not (error FS0002):

let myFn (fn : Func<bool>) = fn.Invoke ()

myFn (fun _ -> false)

Neither does this (error FS0002):

type MyDU = Fn of Func<bool>

Fn (fun _ -> false)

What is the reason for this rather annoying inconsistency?

Upvotes: 8

Views: 405

Answers (1)

kvb
kvb

Reputation: 55184

This behavior is covered by section 8.13.7 (Type-directed Conversions at Member Invocations) of the F# spec, where it states:

Note: These type-directed conversions are primarily for interoperability with existing member-based .NET libraries and do not apply at invocations of functions defined in modules or bound locally in expressions.

Upvotes: 12

Related Questions