Immutable Constant Example

Introduction

The Immutable Constant module allows you to emulate STATIC CONSTANTS.

Lua does not support constants — so this module will raise an error if a programmer (accidentally) tries to update a variable that is designated as a STATIC CONSTANT.

Using the Code [top]

  • Import the Immutable Constant Example channel from the Interfaceware Support repository
  • Review the main.lua and see how ConstantConfigs module is used in the Main function.

Main.lua

ConstantConfigs.lua

This is the github code for the main module:

How it works [top]

In Main.Lua above:

  • Line 2: Imports constants module
  • Line 6: Uses LEFT constants
  • Line 9: Trying to overwrite LEFT constants throws an error in ConstantConfigs.lua

In ConstantConfigs.lua:

  • Line 1 – 9: Readonly function to prevent programmers from updating the constants
  • Line 12 – 16: Defines constant variables

More information [top]