创建与会代表的lambda前pressions在F# [英] Creating Delegates With Lambda Expressions in F#

查看:130
本文介绍了创建与会代表的lambda前pressions在F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么...

 键入IntDelegate =委托为int  - >单元

键入ListHelper =
    静态成员ApplyDelegate(L:INT表)(D:IntDelegate)=
        升|> List.iter(有趣x  - > d.Invoke X)

ListHelper.ApplyDelegate [1..10](有趣x  - > printfn%DX)
 

不能编译的时候:

 键入IntDelegate =委托为int  - >单元

键入ListHelper =
    静态成员ApplyDelegate(L:INT列表,D:IntDelegate)=
        升|> List.iter(有趣x  - > d.Invoke X)

ListHelper.ApplyDelegate([1..10],(有趣x  - > printfn%DX))
 

呢?

这是在第二个, ApplyDelegate 接受它的参数作为一个元组的唯一区别。

  

这个函数有太多的参数,或者被用在上下文中的函数预计不会

解决方案

我没有看过的规格进行确认,但我猜测,从拉姆达的隐式转换到命名的委托类型仅发生在一个成员调用。

您可以随时进行转换明确的:

  ListHelper.ApplyDelegate [1..10](IntDelegate(有趣x  - > printfn%DX))
 

(错误的诊断是很可怜的。我会提交一个bug)

编辑:

有关的书呆子...

是的,规范说:

  

8.13.6类型定向转换的成员调用另外,如   方法应用解决方案(见   §14.4),两型定向转换   被应用在方法调用。

     

如果一个正式的参数是委托   键入DelegateType,与实际   参数是语法功能   值(乐趣...),那么参数是   除preTED,如果它被写   新DelegateType(好玩...)。

这lambda表达式会转换自动地只在成员调用委托类型。在咖喱元件的情况下,通过了第一个参数是一个成员的调用,但随后返回一个函数值来应用第二个参数,函数调用没有这个隐式转换规则。

Why does...

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)

not compile, when:

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))

does?

The only difference that is that in the second one, ApplyDelegate takes its parameters as a tuple.

This function takes too many arguments, or is used in a context where a function is not expected

解决方案

I haven't looked at the spec to confirm, but I am guessing that the implicit conversion from "lambda" to "named delegate type" only occurs in a "member invocations".

You can always make the conversion explicit:

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

(The error diagnostic is quite poor; I'll file a bug.)

EDIT:

For the wonks...

Yeah, the spec says

8.13.6 Type-directed Conversions at member invocations As described in Method Application Resolution (see §14.4), two type-directed conversions are applied at method invocations.

If a formal parameter is of delegate type DelegateType, and an actual argument is syntactically a function value (fun ...), then the parameter is interpreted as if it had been written new DelegateType(fun ...).

that lambdas get converted automagically to delegate types only at "member invocations". In the case of the curried member, the first argument passed is a member invocation, but then that returns a function value to apply the second argument, and function invocations do not have this implicit conversion rule.

这篇关于创建与会代表的lambda前pressions在F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆