理论上, 值类型是不允许拥有可变字段的. 然而, 下面的代码可以通过检查:
#valtype
struct IntCell {
mut contents : Int
}
test {
let cell = { contents: 1 }
cell.contents += 1
assert_eq(cell.contents, 2)
}
并且在除了js和wasm-gc这两个实际上不支持值类型的后端构建时会报出编译器错误:
-- --
/ // / __--------_
/ // /_/ \
--- - \ __
/ X / ____ / )
*_________/__/_____/______/ `--
Oops, the compiler has encountered an unexpected situation.
This is a bug in the compiler.
编译器应当在类型检查阶段就排除mut的不当使用.