本文介绍了FSharp.Core中未记录的"when"关键字用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找有关内联函数的静态解析类型参数的信息时,我偶然发现了 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))

when关键字上方的代码段中可以看出,该格式以以下格式使用:when expr1 = expr2用于各种内置类型.我猜想这是某种等效于如果T = int使用opcode add.ovf,否则如果...,否则执行该操作"的编译器.

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

但是,我在F#文档中找不到对这种语法的单个引用/解释.能够对F#有一定了解的人可以解释上面那段代码中发生的事情吗?

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?

推荐答案

用户卡斯滕(Carsten)对这个答案提供了以下评论,因为他认为这是错误的.

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

Carsten用户为此答案添加了一条附加评论:

User Carsten added an additional comment to this answer:

Carsten的第一条评论中提到的答案是用户John Palmer在2013年4月提出的,该链接指向他在(# ..... #)语法

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?

用户MisterMetaphor提供了一个答案,引用了在论坛上发表的帖子,内容如下:

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

并非如此.此功能的99.9%目的是用于操作 在FSharp.Core.dll中定义(在1.9.2.9及更低版本中称为fslib.dll).

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

有关when关键字的其他用途,请参见以下内容.

For other uses of the when keyword see the following.

此Microsoft文档介绍了使用when关键字来获取匹配的其他条件,匹配表达式(F#).

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

此Microsoft文档介绍了使用when关键字表示通用类型参数的约束,约束(F#).

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

另请参阅此Microsoft文档,该文档描述了在各种设置中与when关键字匹配的模式,模式匹配(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.

匹配表达式"文档连同示例说明了以下内容.

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

这篇关于FSharp.Core中未记录的"when"关键字用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:07