为什么...

type IntDelegate = delegate of int -> unit

type ListHelper =
    static member ApplyDelegate (l : int list) (d : IntDelegate) =
        l |> List.iter (fun x -> d.Invoke x)

ListHelper.ApplyDelegate [1..10] (fun x -> printfn "%d" x)

不编译,当:
type IntDelegate = delegate of int -> unit

type ListHelper =
    static member ApplyDelegate (l : int list, d : IntDelegate) =
        l |> List.iter (fun x -> d.Invoke x)

ListHelper.ApplyDelegate ([1..10], (fun x -> printfn "%d" x))

做?

唯一的区别是在第二个中,ApplyDelegate 将其参数作为元组。

最佳答案

我没有查看规范进行确认,但我猜测从“lambda”到“命名委托(delegate)类型”的隐式转换仅发生在“成员调用”中。

您始终可以使转换显式:

ListHelper.ApplyDelegate [1..10] (IntDelegate(fun x -> printfn "%d" x))

(错误诊断很差;我会提交一个错误。)

编辑:

对于那些...

是的,spec



仅在“成员调用”时,lambda 才会自动转换为委托(delegate)类型。在柯里化成员的情况下,传递的第一个参数是成员调用,但随后返回一个函数值以应用第二个参数,并且函数调用没有这种隐式转换规则。

10-05 22:30