下面的SML/NJ代码为"val (WhatTree)=格伦“提供了一个绑定警告,而不是详尽无遗的警告。F#等效代码不会产生任何警告。为什么?
新泽西标准ML (32位) v110.99.2构建: Tue 9月28日13:04:14 2021
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) = glenF# 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) = glenWhy binding not exhaustive?这个相关问题的公认答案给了我一些关于我的问题的提示。SML警告表示冗余代码。因此,我假设F#编译器编写人员认为这种情况不值得警告。
发布于 2022-01-16 03:27:26
这个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).
https://stackoverflow.com/questions/70726756
复制相似问题