lua - Modules using each other's local objects -


in vanilla lua 5.2, have module contains:

  • two local functions, , b: b call a, call b , call functions stored in c;

  • c: table (local). contains tables contains tables can contain tables... @ end contain functions. these functions may call either or b;

  • then there's return function, d, returned when module loaded using require. call a.

at end, looks quite this:

--don't pay attention functions do: --i writing them down explain how interact each other  local a, b, c  c = {     ...     {         function(a)             b(a)          end     }     ... }  = function(a)     ...     if (...)         b(a)     end     ...     if (...)         c[...]...[...](a)     end     ... end  b = function(a)     a(a) end  return function(s) -- called 1 d     a(s) end 

now, problem this: declaration of c uses own local variables, metatables , stuff, point enclosed declaration in do ... end block.

it - tables inside tables , newlines each curly brace , indentation , on - quite long. wanted put in own module, couldn't access b.

so, question is: there way pass b , maybe file c declared while loading it? mean this, if possible:

--in original module local a, b, c  c = require("c", a, b)  ... 

and then, in c.lua:

local a, b = select(1, ...), select(2, ...)  c = {     ...     {         function(a)             b(a)         end     }     ... } 

i have no idea on how it.

is there way pass variables requiring file required file doesn't involve variables being inserted in global namespace?

main module:

local a, b, c  = function(a)     ...     if (...)         b(a)     end     ...     if (...)         c[...]...[...](a)     end     ... end  b = function(a)     a(a) end  c = require("c")(a, b)  return function(s) -- called 1 d     a(s) end 

c.lua:

local function c_constructor(a, b)     local c =          {             ...             {                 function(a)                     b(a)                 end             }             ...         }     return c end  return c_constructor 

Comments