Mat*_*w H 7 .net f# delegates functional-programming
为什么...
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)
Run Code Online (Sandbox Code Playgroud)
不编译,时间:
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))
Run Code Online (Sandbox Code Playgroud)
呢?
唯一的区别在于,在第二个中,ApplyDelegate将其参数作为元组.
此函数需要太多参数,或者在不期望函数的上下文中使用
Bri*_*ian 11
我没有看过要确认的规范,但我猜测从"lambda"到"named delegate type"的隐式转换只发生在"成员调用"中.
您始终可以明确转换:
ListHelper.ApplyDelegate [1..10] (IntDelegate(fun x -> printfn "%d" x))
Run Code Online (Sandbox Code Playgroud)
(错误诊断很差;我会提交错误.)
编辑:
对于谁...
是的,规范说
8.13.6成员调用中的类型导向转换如方法应用程序解析(参见§14.4)中所述,在方法调用中应用了两个类型导向的转换.
如果形式参数是委托类型DelegateType,并且实际参数在语法上是函数值(fun ...),那么该参数被解释为好像它已经写成新的DelegateType(fun ...).
lambda会自动转换为仅在"成员调用"中委托类型.在curried成员的情况下,传递的第一个参数是成员调用,但是然后返回一个函数值来应用第二个参数,而函数调用没有这个隐式转换规则.
| 归档时间: |
|
| 查看次数: |
1466 次 |
| 最近记录: |