As the title says... Chat on...

User avatar
By faramon
#81748 Hi there,
one big question about modular files lua...

I have some files:
--------------------
config.lua
--------------------
local module = {}

module.UN = 'user'
module.PS = 'pass'

local function doSomething()
return 'a'
end

return module
---------------------


------------------------------------
app.lua
------------------------------------
local module = {}

a = config.doSomething()
myUser = config.UN
myPass = config.PS

return module
------------------------------------

-------------------------------
init.lua
-------------------------------
config = require("config")
app = require("application")
-------------------------------

what does local module = {} in all scripts mean, is this all together in one big module scope?
My app.lua must have some application logic. config.lua is some configuration variable and init.lua is startup script.
Is this ok to have in init? what this mean, will app and config have own thread and is this correct that app does not need to have call config = require("config") again when init is calling it? I am confused. Can somebody explain how this modular lua works doing application within modules?
If I do use "require" with some others scripts that does some other part of aplication functionality will those scripts do their "homework" in parallel with app.lua?

Thanx
User avatar
By marcelstoer
#81778 I don't think your example even works.

doSomething() in config.lua is not bound to module scope but "privat"/local to the module. Hence, it's not accessible from outside.

Code: Select allmodule.UN = 'user'
is different in that respect. If you do require("config") you'll then have access to UN.

Look at how some of the Lua modules in the firmware repository work e.g. https://github.com/nodemcu/nodemcu-firm ... bh1750.lua:
Code: Select allfunction M.read()
is a public function while
Code: Select alllocal function read_lux()
is a private function