Why is a recursive Lua function calling itself via a local alias instead of directly?
14:14 26 Jan 2026

Below code is from nmap tableaux.lua script file which is located at /usr/share/nmap/nselib

local tcopy_local

function tcopy(t)
  local tc = {}
  for k, v in pairs(t) do
    if type(v) == "table" then
      tc[k] = tcopy_local(v)
    else
      tc[k] = v
    end
  end
  return tc
end

tcopy_local = tcopy

This function recursively copies a table. What confuses me is the use of tcopy_local.

tcopy is already recursive, and in other languages it would be used

 tc[k] = tcopy(v)

Instead, the code declares a local variable (tcopy_local) and assigns it to tcopy after the function is defined, and the recursion goes through that local variable.

Why is this pattern used? I’d like to understand the reasoning behind this pattern and when it’s preferable to direct recursion.

recursion lua nmap