简体   繁体   中英

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 .

However I don't get the first part ^a , nor to I get the : (member Name : string) .

All I know is that the last arg is applying the value of arg of the ^a : (member Name : string) function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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