E4144

E4144#

这是一个常量,不是构造函数,不能应用于参数。

当你在模式中把常量当作构造函数使用时,会发生此错误。可能的原因是你有一个常量与其他包中的构造函数同名;这种情况下需要使用限定名或类型注解来消除歧义。

错误示例#

pub const Value : Int = 1

pub fn classify(value : Int) -> String {
  match value {
    Value(_) => "Value"
  //^~~~~
  // Error: 'Value' is a constant, not a constructor, it cannot be applied to arguments.
    _ => "Other"
  }
}

建议#

如果要匹配常量,可以从模式中移除载荷。

pub const Value : Int = 1

pub fn classify(value : Int) -> String {
  match value {
    Value => "Value"
    _ => "Other"
  }
}

或者,如果常量与其他包中的构造函数同名,您可以使用限定名或类型注释来消除歧义。

pub fn classify_qualified(item : @a.Item) -> String {
  match item {
    @a.Value(_) => "Value"
  }
}