E4087

E4087#

Mutating something immutable, such as a variable or struct field.

MoonBit requires programmers to explicitly declare mutable variables and struct fields. Notice that in MoonBit, variable mutability only refers to the variable itself, not the value it holds. So when you declare an immutable let value of type Array[T], Ref[T], or a struct with mutable fields, you can still mutate the inner structure, but cannot replace the value of the whole variable.

Tuples are always immutable. If you want mutability, use a struct with mutable fields instead.

Erroneous Example#

Variable mutation:

///|
test {
  let a = 0
  a = 1
}

Struct field mutation:

///|
priv struct S {
  x : Int
}

///|
test {
  let s = { x: 1 }
  println(s.x)
  s.x = 2
}

Suggestion#

To fix this error, you need to declare the variable as mutable by adding the mut keyword before the variable name or struct field name.

///|
test {
  let mut a = 0
  a = 1
  println(a)
}

///|
priv struct S {
  mut x : Int
}

///|
test {
  let s = { x: 1 }
  s.x = 2
  println(s.x)
}