FSharp.Core中未记录的"when"关键字用法 [英] Undocumented `when` keyword usage in FSharp.Core

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

问题描述

在寻找有关内联函数的静态解析类型参数的信息时,我偶然发现了

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.

问题是:when照原样使用,与此处无关 有记录的用法-似乎被称为静态有条件的 优化,不应在核心库之外使用- 确实继续尝试使用它-您将看到,除非您不能 您使用约翰回答(另一个问题)中提到的技巧

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)

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

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

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?

您可以实际使用它,但必须指定 代码中的--compiling-fslib(未记录)和--standalone标志.

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

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

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

以F#代码嵌入的IL.正式支持此功能吗?

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

并非如此.此功能的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关键字表示通用类型参数的约束,

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

另请参阅此Microsoft文档,该文档描述了在各种设置中与when关键字匹配的模式,

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.

模式是用于转换输入数据的规则.他们被使用 整个F#语言中,以比较具有逻辑结构的数据或 结构,将数据分解为组成部分或提取 从数据中获取信息的方式多种多样.

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.

您可以使用when子句来指定附加条件,即 变量必须满足以匹配模式.提到这样的条款 作为后卫. when关键字后面的表达式不计算 除非匹配与该后卫相关的模式.

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆