types.Array.is_frozen
Method on every
arrayis_frozen(): any
Reports whether this array reference is frozen.
let squad = freeze(["healer", "tank"]);
println("squad.is_frozen() =", squad.is_frozen());
println("[].is_frozen() =", [].is_frozen());Output:
squad.is_frozen() = true
[].is_frozen() = falseReturn value
true if this reference is immutable, false otherwise.
Notes
freeze marks a reference immutable, not the underlying array: it returns a new, frozen alias and leaves the reference it was given alone. Two variables that alias the same array can disagree about is_frozen, and mutating through the unfrozen one is visible through the frozen one too, since both still point at one array; only the immutable alias itself refuses to be the one doing the mutating.
Example
let a = [1, 2, 3];
let f = freeze(a);
println("a.is_frozen() =", a.is_frozen()); // freeze() froze the alias `f`, not the variable `a`
println("f.is_frozen() =", f.is_frozen());
println("a == f:", a == f); // still the very same array underneath
a.append(4); // mutating through the unfrozen alias works
println("f.len() =", f.len()); // f sees it too, they are one object
try { f.append(5) } catch (e) { println("f.append(5) throws:", e); }Output:
a.is_frozen() = false
f.is_frozen() = true
a == f: true
f.len() = 4
f.append(5) throws: Cannot modify immutable objectSee also
freeze | Returns a reference to obj with the immutable flag set. |
clone | Makes a shallow copy of the array. |
| array | class index |