E4197

E4197#

编译器诊断名称:cannot_match_struct_constr

struct 声明的构造器是构造函数,而不是模式构造器。它可以被调用来创建值,但不能在 match 模式中使用。

错误示例#

///|
priv struct Point {
  x : Int

  fn new(Int) -> Point
}

///|
fn Point::new(x : Int) -> Point {
  { x, }
}

///|
fn read(point : Point) -> Int {
  match point {
    Point(x) => x
  }
}

///|
test {
  let _ = read(Point(1))
}

建议#

直接读取结构体字段,或使用另一种不会调用构造函数的模式。

///|
priv struct Point {
  x : Int

  fn new(Int) -> Point
}

///|
fn Point::new(x : Int) -> Point {
  { x, }
}

///|
fn read(point : Point) -> Int {
  point.x
}

///|
test {
  inspect(read(Point(3)), content="3")
}