freeze()

Published Sep 1, 2023
Contribute to Docs

The .freeze() function in Lua freezes the provided table, and makes it read-only.

Syntax

table.freeze(f)

The .freeze() function attempts to freeze a specified non-frozen table named f. The function willย fail if fย is not a table or is already frozen.

Example

Consider the following table:

local frozen_table = table.freeze({
"๐ŸŽ",
"๐ŸŒ",
"๐Ÿ‡",
"๐Ÿ“",
"๐Ÿ‰"
})

The variable frozen_table is instantiated with the .freeze() method to make the contents read-only.

Now, if an element is added:

frozen_table[1] = "๐Ÿ”"

The code will return the following output:

Error: Frozen table cannot be modified

The error is thrown because the method has made the table read-only, so the contents cannot be modified.

All contributors

Looking to contribute?

Learn Lua on Codecademy