E4094#
Cannot modify a read-only field.
For a read-only struct, you cannot modify the value of a field using
assignment outside of the package where the type is defined.
Erroneous example#
Suppose you have a package a in module username/hello:
a/moon.pkg:
a/a.mbt:
///|
pub struct T {
mut value : Int
}
///|
pub fn T::new() -> T {
T::{ value: 0 }
}
///|
pub fn T::set(self : T, value : Int) -> Unit {
self.value = value
}
And you have another package b in the same module:
b/moon.pkg:
import {
"moonbit-community/E4094/a",
}
b/b.mbt:
///|
test {
let a = @a.T::new()
a.value = 3 // Error: Cannot modify a read-only field: value
}
Suggestion#
If you have control over the package where the type is defined, expose mutation through a method in that package.
a/a.mbt:
///|
pub struct T {
mut value : Int
}
///|
pub fn T::new() -> T {
T::{ value: 0 }
}
///|
pub fn T::set(self : T, value : Int) -> Unit {
self.value = value
}
b/b.mbt:
///|
test {
let a = @a.T::new()
a.set(3)
}