【问题标题】:Binding not exhaustive warning in SML/NJ but not in F# for same pattern在 SML/NJ 中绑定不是详尽的警告,但在 F# 中没有针对相同模式
【发布时间】: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 编译器会产生相同的警告,或者它们都不会产生警告。
  • 我已经更新了答案以展示如何执行绑定而不是创建函数

标签: f# sml smlnj


【解决方案1】:

这个 F# 代码let Grove(whatTree) = glen 是模棱两可的,因为它可以解释为与解构或函数的值绑定。

第一种情况的语法是

let pattern = expr

以秒为单位的语法是

let name pattern-list = expr

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

最后:SML 和 F# 中的代码做不同的事情,这就是为什么没有警告


要实际执行绑定,let 的左侧应该用括号括起来:

let (Grove(whatTree)) = glen

它会产生警告: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).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    相关资源
    最近更新 更多