Undocumented `when` keyword usage in FSharp.Core

Looking for information about statically resolved type parameters for inline functions I stumbled upon the definitions of various primitive operators in FSharp.Core:

let inline (+) (x: ^T) (y: ^U) : ^V = 
     CheckedAdditionDynamic<(^T),(^U),(^V)>  x y 
     when ^T : int32       and ^U : int32      = (# "add.ovf" x y : int32 #)
     when ^T : float       and ^U : float      = (# "add" x y : float #)
     // <snip>
     when ^T : ^T = ((^T or ^U): (static member (+) : ^T * ^U -> ^V) (x,y))

As can be seen in the snippet above the when keyword is used in the format of: when expr1 = expr2 for various built-in types. I'm guessing that this is some sort of compiler equivalent of "if T=int use opcode add.ovf, else if ..., else do that".

However, I could not find a single reference/explanation to this kind of syntax in the F# documentation. Could someone with some inside knowledge of F# explain what is going on in that snippet above?


User Carsten has provided the following comment to this answer as he considers it to be wrong.

the thing is: when used as is here has nothing to do with the documented usages - it seems to be called static conditional optimization and should not be used outside the core libraries - indeed go on and try to use it - you will see that you cannot unless you use the tricks mentioned in Johns answer (other question)

User Carsten added an additional comment to this answer:

I added a comment - I don't think my educated guess is worth an answer - I hoped that one of the insiders hanging around would finally put an official answer to it

The answer referred to in Carsten's first comment is by user John Palmer in April 2013 which links to this answer he provided on the (# ..... #) syntax, What is the (# ... #) syntax seen in F3 standard library implementation?

You can actually use this but you have to specify the --compiling-fslib (undocumented) and --standalone flags in your code.

User MisterMetaphor provided an answer quoting a posting in a forum that said the following:

Embedded IL in F# codes. Is this feature officially supported?

Not really. The 99.9% purpose of this feature is for operations defined in FSharp.Core.dll (called fslib.dll in 1.9.2.9 and before).

For other uses of the when keyword see the following.

This Microsoft document describes using the when keyword for additional conditions on matching, Match Expressions (F#).

This Microsoft document describes using the when keyword to express constraints for generic type parameters, Constraints (F#).

Also see this Microsoft document describing pattern matching with the when keyword in various settings, Pattern Matching (F#).

The Pattern Matching document says the following along with several examples.

Patterns are rules for transforming input data. They are used throughout the F# language to compare data with a logical structure or structures, decompose data into constituent parts, or extract information from data in various ways.

The Match Expression document says the following along with an example.

You can use a when clause to specify an additional condition that the variable must satisfy to match a pattern. Such a clause is referred to as a guard. The expression following the when keyword is not evaluated unless a match is made to the pattern associated with that guard.

链接地址: http://www.djcxy.com/p/80662.html

上一篇: F#“​​非托管”类型约束的行为

下一篇: 在FSharp.Core中未记录`when'关键字的使用情况