E3012

E3012#

Struct pattern and map pattern cannot be mixed. The key in map pattern must be a literal, while the key in struct pattern must be the field name.

Erroneous example#

struct S {
  value : Int
}

pub fn S::get(self : S, index : String) -> Int? {
  if index == "value" {
    return Some(self.value)
  }
  return None
}

test {
  let s : S = { value: 42 }
  match s {
    { "value": value, value, .. } => println("Value is: \{value}") // Error: Struct pattern and map pattern cannot be mixed.
    _ => println("No value")
  }
}

Suggestion#

Remove either the map pattern part or the struct pattern part from the pattern.

///|
pub(all) struct S {
  value : Int
}

///|
pub fn S::get(self : S, index : String) -> Int? {
  if index == "value" {
    return Some(self.value)
  }
  return None
}

///|
fn use_map_pattern(s : S) -> Unit {
  match s {
    { "value": value, .. } => println("Value is: \{value}")
    _ => println("No value")
  }
}

///|
test {
  let s : S = { value: 42 }
  use_map_pattern(s)
  use_struct_pattern(s)
}

Or,

///|
fn use_struct_pattern(s : S) -> Unit {
  match s {
    { value } => println("Value is: \{value}")
  }
}