Menu
Coddy logo textTech

Recap - Read-Only Table

Part of the Logic & Flow section of Coddy's Lua journey — lesson 23 of 54.

challenge icon

Challenge

Easy

Write a function makeReadOnly that takes original and returns a read-only version of the table.

Create a proxy table with a metatable that allows reading from the original table but prevents any modifications.

Logic:

  • Create an empty proxy table
  • Create a metatable with __index set to the original table (to allow reading)
  • Add __newindex to the metatable that does nothing (to block modifications)
  • Attach the metatable to the proxy using setmetatable()
  • Return the proxy table

Parameters:

  • original (table): The table to protect from modifications

Returns: A proxy table that provides read-only access to the original table (table)

Try it yourself

function makeReadOnly(original)
    -- Write code here
end

All lessons in Logic & Flow