Asik
Asik

Reputation: 22133

Please explain this syntax

From an answer by kvb about how to call implicit conversion operators:

let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)

I've known F# for a while but I just don't know how to parse the implementation here. What is (^a or ^b)? And the stuff after that? Please go over what each part represents grammatically.

Upvotes: 2

Views: 168

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80734

^a or ^b means literally "^a or ^b".

The colon : means "has" or "contained in", depending on how you look at it.

So the whole expression (^a or ^b) : (static member op_Implicit : ^a -> ^b) means "static member named "op_Implicit" that has type ^a -> ^b and is defined on either type ^a or type ^b". This whole expression ultimately evaluates to a function of type ^a -> ^b.

Then, the x placed to the right of that expression means "function application", just like in the usual F# syntax.

So the whole thing taken together would mean "on type ^a or type ^b, find a static member named "op_Implicit" that has type ^a -> ^b, and apply that member to argument x".

For a bit more discussion of statically resolved constraints, see this answer or this MSDN article.

Upvotes: 7

Related Questions