【发布时间】:2022-01-16 21:32:09
【问题描述】:
下面的 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
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
Why binding not exhaustive? 这个相关问题的接受答案给了我一些关于我的问题的提示。 SML 警告指示冗余代码。所以,我假设 F# 编译器作者认为这种情况不值得警告。
【问题讨论】:
-
您希望编译器检查什么?
-
我预计 F# 和 SML/NJ 编译器会产生相同的警告,或者它们都不会产生警告。
-
我已经更新了答案以展示如何执行绑定而不是创建函数