对于相同模式,在 SML/NJ 中绑定不详尽的警告,但在 F# 中不绑定

Lea*_*erX 1 f# sml smlnj

下面的 SML/NJ 代码会生成“val Grove(whatTree) = glen”的绑定非详尽警告。F# 等效代码不会产生警告。为什么?

新泽西州标准 ML(32 位)v110.99.2 [构建时间:2021 年 9 月 28 日星期二 13:04:14]:

datatype tree = Oak|Elem|Maple|Spruce|Fir|Pine|Willow
datatype vegetable = Carrot|Zucchini|Tomato|Cucumber|Lettuce
datatype grain = Wheat|Oat|Barley|Maize
datatype plot = Grove of tree|Garden of vegetable|Field of grain|Vacant
val glen = Grove(Oak)
val Grove(whatTree) = glen
Run Code Online (Sandbox Code Playgroud)

F# 6.0.0 警告级别 5:

type Tree = Oak|Elem|Maple|Spruce|Fir|Pine|Willow
type Vegetable = Carrot|Zucchini|Tomato|Cucumber|Lettuce
type Grain = Wheat|Oat|Barley|Maize
type Plot = Grove of Tree|Garden of Vegetable|Field of Grain|Vacant
let glen = Grove(Oak)
let Grove(whatTree) = glen
Run Code Online (Sandbox Code Playgroud)

为什么绑定不详尽? 这个相关问题的公认答案给了我一些关于我的问题的提示。SML 警告指示冗余代码。因此,我假设 F# 编译器编写者认为这种情况不值得警告。

JL0*_*0PD 5

此 F# 代码let Grove(whatTree) = glen不明确,因为它可以被解释为与解构或函数的值绑定。

在第一种情况下语法是

let pattern = expr
Run Code Online (Sandbox Code Playgroud)

以秒为单位的语法是

let name pattern-list = expr
Run Code Online (Sandbox Code Playgroud)

由于 F# 支持遮蔽,因此创建新函数是合法的。但SML对此似乎有不同意见,决定绑定价值。

最后:SML 和 F# 中的代码执行不同的操作,这就是没有警告的原因


要实际执行绑定,左侧let应加括号:

let (Grove(whatTree)) = glen
Run Code Online (Sandbox Code Playgroud)

它会产生警告:C:\stdin(6,5): warning FS0025: Incomplete pattern matches on this expression. For example, the value 'Field (_)' may indicate a case not covered by the pattern(s).