Lua - Immutable Arrays



In Lua, an array is implemented by table. A table is by default mutable. Consider the following case where we've an array of four directions.

main.lua

-- an array of directions
directions = { "UP", "DOWN", "LEFT", "RIGHT"}

-- modify an existing direction
directions[3] = "LEFTMOST"

-- add new direction
directions[5] = "RIGHTMOST"

-- print updated values
print(directions[3])
print(directions[5])

Output

When we run the above program, we will get the following output−

LEFTMOST
RIGHTMOST

Here we can see that array is mutable. Now there is no direct way to make a array immutable but we can implement the same to some extent using metatables.

Define a function to get readonly array

function readonly(table)
   return setmetatable({}, {
      -- set the index as provided table
      __index = table,
	  
	  -- prevent new index to be added
      __newindex = function(table, key, value)
         error("ReadOnly Array. Permission denied.")
      end,
	  
	  -- setmetatable, getmetatable methods are not allowed
      __metatable = false
   });
end

Get the readonly table

-- readonly array of directions
directions = readonly({ "UP", "DOWN", "LEFT", "RIGHT"})

Complete Example of readonly/immutable array

In following example, let's check the normal functionality of the array and check if modifications to readonly array is allowed using index notation or not.

main.lua

function readonly(table)
   return setmetatable({}, {
      -- set the index as provided table
      __index = table,
	  
	  -- prevent new index to be added
      __newindex = function(table, key, value)
         error("ReadOnly Array. Permission denied.")
      end,
	  
	  -- setmetatable, getmetatable methods are not allowed
      __metatable = false
   });
end

-- readonly array of directions
directions = readonly({ "UP", "DOWN", "LEFT", "RIGHT"})

-- modify an existing direction
directions[3] = "LEFTMOST"

-- add new direction
directions[5] = "RIGHTMOST"

-- print updated values
print(directions[3])
print(directions[5])

Output

When we run the above program, we will get the following output−

lua: main.lua:8: ReadOnly Array. Permission denied.
stack traceback:
	[C]: in function 'error'
	main.lua:8: in metamethod 'newindex'
	main.lua:23: in main chunk
	[C]: in ?

Example - Using rawset

But we can still modify the above array using rawset() method as shown below −

main.lua

function readonly(table)
   return setmetatable({}, {
      -- set the index as provided table
      __index = table,
	  
	  -- prevent new index to be added
      __newindex = function(table, key, value)
         error("ReadOnly Array. Permission denied.")
      end,
	  
	  -- setmetatable, getmetatable methods are not allowed
      __metatable = false
   });
end

-- readonly array of directions
directions = readonly({ "UP", "DOWN", "LEFT", "RIGHT"})

-- modify an existing direction
rawset(directions,3,"LEFTMOST")

-- print updated value
print(directions[3])

Output

When we run the above program, we will get the following output−

LEFTMOST
Advertisements