为什么不对记录的属性进行模式匹配编译? [英] Why doesn't pattern matching on a property of a record compile?
本文介绍了为什么不对记录的属性进行模式匹配编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么不对记录的属性进行模式匹配?
type Cell = { X:int; Y:int }
let isNeighbor cell1 cell2 =
match cell1.X, cell2.X with
| cell1, cell2 when cell1.X <> cell2.X
|| cell1.Y <> cell2.Y -> let xAligned = IsValueNeighbor cell1.X cell2.X
let yAligned = IsValueNeighbor cell1.Y cell2.Y
xAligned && yAligned
| _ -> false
错误:
类型'int'与类型'Cell'不兼容
The type 'int' is not compatible with the type 'Cell'
以下表达式似乎有问题:
The following expression appears to have an issue:
cell1.X <> cell2.X
|| cell1.Y <> cell2.Y
推荐答案
执行时
match cell1.X, cell2.X with
| cell1, cell2
您创建了一个新的cell1
变量,它是cell1.X
(一个整数).
you have created a new cell1
variable, which is cell1.X
(an int).
我可能只是在这里使用了if,或者将其更改为| _,_ when ...
I would probably have just used an if here, or change to | _,_ when ...
这篇关于为什么不对记录的属性进行模式匹配编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文