假设我有两种类型,Vector3D
和Vector2D
,并且它们被标记了(这是正确的术语,对吗?),我想编写一个仅在vector(two)
上运行的函数(或者type two;
type three;
type vector('a) =
| Vector2D(float,float): vector(two)
| Vector3D(float,float,float): vector(three);
let first = (a: vector(two)) => {
switch(a) {
| (x,y) => x +. y
}
}
let second = (Vector2D(x,y)) => {
x +. y
}
let third = ((x,y): vector(two)) => {
x +.y
}
在这里更正确吗?),就像这样:
first
second
和Vector3D
函数禁止传递first
,就像我想要的那样,但是它们会发出警告“此模式匹配并不详尽”。
对于Vector2D
,我想知道为什么这还不详尽,是否在这里我不将可能的选项限制为second
?对于first
,我想原因与third
相同,但是用这种语法怎么可能解决这个问题呢?
对于type state = Solid | Liquid
let splash = (object) => {
switch(object) {
| Liquid => "splashing sounds. I guess."
| Solid => "" // this should not even be possible in the context of this function,and I want to compiler to enforce this
}
,由于“此模式与('a,'b)匹配,但预期为vector(two)”,因此该代码无法编译。为什么编译器期望这里有任何元组?不能在函数参数中使用解构吗?
编辑:
事实证明,还有一个更简单的问题来证明我想要的东西
{{1}}