简体   繁体   English

F#语法解释(^)

[英]F# syntax explanation (^)

Well, sorry for the bad title. 好吧,抱歉糟糕的头衔。

Consider the following function: 考虑以下功能:

let inline getName arg =
    ( ^a : (member Name : string) arg)

I know what it does, but I know that only because I copy pasted it. 我知道它的作用,但我知道这只是因为我复制粘贴它。

It says: given an arg return the name member of arg . 它说:给予arg返回name成员arg

However I don't get the first part ^a , nor to I get the : (member Name : string) . 但是我没有得到第一部分^a ,也没有得到: (member Name : string)

All I know is that the last arg is applying the value of arg of the ^a : (member Name : string) function. 我所知道的是,最后一个arg正在应用^a : (member Name : string)函数的arg值。

The ^ is used to specify that the type parameter is statically resolved , this means that the type will be resolved at compile time, not at run time. ^用于指定类型参数是静态解析的 ,这意味着类型将在编译时解析,而不是在运行时解析。

The second part is a generic constraint that specifies that the type must have a member called Name with the given signature, in this case a string property. 第二部分是一个通用约束 ,它指定该类型必须具有名为Name的成员,该成员具有给定签名,在本例中为字符串属性。 The syntax you show is how to actually call the member and the compiler is inferring the generic constraint on the function, but you could also specify the constraint explicitly, although there is no need. 您显示的语法是如何实际调用该成员,并且编译器正在推断该函数的泛型约束,但您也可以明确指定约束,尽管不需要。

let inline getName (arg : ^a when ^a : (member Name : string)) =
    ( ^a : (member Name : string) arg)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM