如何将参数限制为变量类型的一个变量

问题描述 投票:0回答:1

假设我有两种类型,Vector2DVector3D,并且它们被标记了(这是正确的术语,对吗?),我想编写一个仅对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
}

firstsecond函数禁止传递Vector3D,正如我想要的,但它们会发出警告“此模式匹配并不详尽”。

对于first,我想知道为什么这还不是很详尽,在这里,我是否不将可能的选项限制为Vector2D?对于second,我想原因与first相同,但是用这种语法怎么可能解决这个问题呢?

关于third,由于“此模式与('a,'b)匹配,但预期为vector(two)”,因此未编译该代码。为什么Reasonml在这里期望有任何元组?无法在函数参数中使用解构吗?

编辑:原来还有一个更简单的问题来证明我想要的东西

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
}
pattern-matching ocaml algebraic-data-types gadt reason
1个回答
0
投票

您可以使用polymorphic variants完成所需的操作:

© www.soinside.com 2019 - 2024. All rights reserved.