20

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?

Mart Roosmaa
  • 928
  • 8
  • 10
  • 2
    Note that you cannot use this syntax (or the `(# ... #)` syntax) in standard F#. – Ramon Snir Oct 16 '14 at 14:33
  • 5
    Here is some of the explaination: http://stackoverflow.com/questions/15968054/what-is-the-syntax-seen-in-f-standard-library-implementation/15968212#15968212 – John Palmer Oct 17 '14 at 00:15
  • 1
    it seems to be called static conditional optimization and I think it is not supposed to be used outside the core libraries - you can find out if you try to use it and inspect the compiler errors ;) – Random Dev Jun 25 '15 at 14:59

1 Answers1

2

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.

Community
  • 1
  • 1
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • do you have enough rep to see the deleted questions? Because I could copy&paste my comment to the last one here as well ... what you describe here is not what's happening in the OPs question (indeed the comments there answer it already) - also you should not (only) post links – Random Dev Jun 25 '15 at 13:46
  • @Carsten, I see only a question, a couple of comments, none of which appear to answer the OP's question about the `when` keyword usage though one does link to an answer about the .NET opcode insertion used in the F# core, a usage that is now quite rare and should not be used otherwise. Links have always been perfectly acceptable when pointing to other documentation as long as a pertinent section is quoted as well. Why don't you copy your comment since I see nothing from you other than this comment. – Richard Chambers Jun 25 '15 at 14:49
  • 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) – Random Dev Jun 25 '15 at 14:53
  • @Carsten, I suggest that you create an answer from this or add it to the comments then. – Richard Chambers Jun 25 '15 at 14:57
  • 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 – Random Dev Jun 25 '15 at 15:00
  • btw: I don't mind if you just add it to your answer - indeed it might be a good idea as we then have some answer at last and can maybe close this – Random Dev Jun 25 '15 at 15:07