6
为什么...创建委托和Lambda表达式在F#
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
将其参数作为元组。
这个函数的参数太多,或者在没有预期的功能
谢谢。 “成员调用”到底是什么以及为什么有元组参数会有所作为? 成员应该总是采用tupled参数吗? – 2010-05-06 18:52:48
“成员”是你用'member'关键字定义的东西,例如,某些类型的静态或实例方法。这些与通过'let f x = ...'或'fun'定义的“函数”形成对比,这些函数相对而言非常严格(例如,您不能重载函数,但可以重载成员)。成员们应该总是采取tupled的论据,是的;与特定于F#的函数相比,成员更多的是.NET-y实体。 – Brian 2010-05-06 18:59:57
'tupled arguments'有所不同,因为当它被元组化时,所有的参数都立刻被传递给成员,所以第二个参数是成员的一个参数。在curried参数的情况下,第一个参数是成员的一个参数,并且导致一个新的(非成员)函数值,然后接受第二个参数。 – Brian 2010-05-06 19:03:40