首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SML/NJ中绑定并非详尽的警告,但在F#中绑定相同的模式

在SML/NJ中绑定并非详尽的警告,但在F#中绑定相同的模式
EN

Stack Overflow用户
提问于 2022-01-16 01:28:22
回答 1查看 83关注 0票数 1

下面的SML/NJ代码为"val (WhatTree)=格伦“提供了一个绑定警告,而不是详尽无遗的警告。F#等效代码不会产生任何警告。为什么?

新泽西标准ML (32位) v110.99.2构建: Tue 9月28日13:04:14 2021

代码语言:javascript
复制
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:

代码语言:javascript
复制
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#编译器编写人员认为这种情况不值得警告。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-16 03:27:26

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

在第一个例子中,语法是

代码语言:javascript
复制
let pattern = expr

在秒内,情况语法是

代码语言:javascript
复制
let name pattern-list = expr

因为F#支持隐藏,所以创建新函数是合法的。但是SML似乎对此有不同的看法,并决定绑定值。

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

要实际执行绑定,let的左侧应插入括号:

代码语言:javascript
复制
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).

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70726756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档