In Scene A, we require Module X twice by 2 local variables modA and modB:
local modA = require("moduleX") local modB = require("moduleX") modA.new() modB.new() modA.printIt () modB.printIt ()
Module X looks like:
local M = {} function M.new () local count = 0 function M.printIt () count = count + 1 print("count = ", count) end end return M
I would have expected this to output “count = 1” twice as there are 2 versions of count each one local to modA and modB. But it prints “count = 1” and “count = 2”. So not sure what’s going on. Firstly why is this the case and secondly how to achieve the original expectation?
Many thanks.