Quirrel4.41.0

types.Array.is_frozen

Method on every array
is_frozen(): any

Reports whether this array reference is frozen.

examples/types/array/is_frozen-basic.nut
let squad = freeze(["healer", "tank"]);
println("squad.is_frozen() =", squad.is_frozen());
println("[].is_frozen() =", [].is_frozen());
Output:
squad.is_frozen() = true
[].is_frozen() = false

Return 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

examples/types/array/is_frozen.nut
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 object

See also

freezeReturns a reference to obj with the immutable flag set.
cloneMakes a shallow copy of the array.
arrayclass index