Lua - Module Scope



Module Scope is a fundamental way to organize and reuse code. A Module is a self contained entity which prevents naming conflicts and promotes good programming concepts like encapsulation of data, private functions and public functions to access the private entities.

In Lua, module achieves encapsulation with table.

Syntax

-- a table to represent the module
local module_table = {}

-- variable accessible only within module
local private_variable

-- publically accessible function via module
function module_table.public_function
   -- access private variable and function
end

-- function only accessible within module, publically inaccessible
function private_function
   -- access private variable
end

-- return the table as module
return module_table

Example - Usage of Module Scope

Let's define a module file utility.lua as described below −

utility.lua

-- a table to represent the module
local utility = {}

-- variable accessible only within module
local secret = 42

-- publically accessible function via module
function utility.generatePassword(username)
   local value = generateSecret(username)
   return value
end

-- function only accessible within module, publically inaccessible
function generateSecret(name)
   return string.len(name) + secret
end

-- return the table as module
return utility

Now, in order to access factory function from Lua module in another file, say, moduletutorial.lua, you need to use the following code segment.

moduletutorial.lua

-- load the module
utility = require("utility")

-- call the public function from the module
local password = utility.generatePassword("Robert")

-- variable accessible only within module
local secret = 42

-- trying to access private function or private variable will result in error
print(utility.generateSecret("Robert"))
-- print(utility.secret)

Output

In order to run this code, we need to place the two Lua files in the same directory or alternatively, you can place the module file in the package path and it needs additional setup. When we run the above program, we will get the following output−

48
lua: moduletutorial.lua:11: attempt to call field 'generateSecret' (a nil value)
stack traceback:
      moduletutorial.lua:11: in main chunk
         [C]: ?

Explanation

utility.lua

  • We've created an empty table utility as a module.

  • secret and generateSecret are local variable and local function and are accessible within module only and are private in scope.

  • utility.generatePassword(username) is public in scope and is accessible via utility module.

  • In the end, we're returning the utility table as a module.

moduletutorial.lua

  • utility = require("utility") statement is used to load the module and assign to utility variable.

  • As next, we're accessing utility module's public function using dot(.) notation and corresponding result is printed.

  • If we try to access the private variable, or private function, Lua throws an error.

Key Aspects of Scope in a Module

  • Local Variables − a private variable is created using local keyword within a module. A local variable has scope limited to the block in which it is defined. In this case, it is the module thus making the variable private within module.

  • Public Function − a public function is defined as a table field. When a module is returned, only fields of the table are accesible thus making it public in scope.

  • Private Function − a private function is defined within module file but not as table field. When a module is returned, only fields of the table are accesible thus private function is not accessible.

Conclusion

A module scope helps in creating private and public scopes by creating local variables within module are private and by creating a table which exposes its members as public elements. Using this approach, we can build clean, organized and maintainable code.

Advertisements