🎓 A1Learner
🔍
0 of 49 lessons completed

Lua Module Versioning

Module versioning is a version label carried on a module table. Old names stay as aliases so older code keeps working.

A module carries its version

The version sits in one field on the module table. Reading it shows the release in use.

local calc = { _VERSION = "2.0.0", double = function(x) return x * 2 end }
print(calc._VERSION)

An old name becomes an alias

The old name is set to the new function. Calling the old name runs the new code.

local calc = { _VERSION = "2.0.0", double = function(x) return x * 2 end }
calc.twice = calc.double
print(calc.twice(21))

Both names give the same answer

An alias is the same function, not a copy. The two calls agree exactly.

local calc = { _VERSION = "2.0.0", double = function(x) return x * 2 end }
calc.twice = calc.double
print(calc.double(6), calc.twice(6))

Example

local mathbox = { _VERSION = "3.1.0", add = function(a, b) return a + b end }
mathbox.sum = mathbox.add
print(mathbox._VERSION)
print(mathbox.add(20, 22))
print(mathbox.sum(20, 22))

Key points

  • Version field: a module table carries its version in _VERSION.
  • Alias: an old function name points at the same new function.
  • Same function: both names call the same code, so results match.
  • Check: code can read the version field before using the module.
  • Upgrade: a new version can add names without removing old ones.

In this example, the module table carries version 3.1.0 and an add function. The old name sum is set to the same function. Both names return 42, so old callers and new callers get the same answer.

Note

version text is for people and checks, not a rule Lua enforces.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which field holds a module version?