On Feb 18, 2013, at 09:59 AM, Justin Cormack <justin@specialbusservice.com> wrote:
all requires should be assigning to a local variable.
[...]
local scxml = {}
scxml.state = require "lib/state"
scxml.event = require "lib/event"
return scxml
This works for the simplest cases I supplied where each file adds exactly one completely independent table to the master. However, I have three more cases that aren't quite as clean:
* Multiple Datatypes per File
* Multiple Files Augmenting the Same Table
* One File Referencing Tables from Another
1) Multiple Datatypes per File
One of the file declares a few (currently-global) new datatypes that are used by various methods throughout. Would you recommend:
local LXSC = {}
...require others...
for name,t in pairs(require('lib/datatypes') do LXSC[name]=t end
return LXSC
# lib/datatypes.lua
local Queue = {}; ...
local OrderedSet = {}; ...
local List = {}; ...
return {Queue=Queue, OrderedSet=OrderedSet, List=List}
This is easily worked around by making one file per 'object', but that feels like an ugly burden. Further, while this makes these classes available on the master LXSC table, there is no way for the sub-tables to access this master. Which brings me more explicitly to problem #2:
2) Multiple Files Augmenting the Same Table
Some of the files do not add a new table, but augment another existing table. For example:
LXSC = {}
require 'lib/scxml'
require 'lib/runtime'
LXSC.SCXML = {}
function LXSC.SCXML:go() ... end -- modify a table defined elsewhere
If there was only one case like this, I could 'invert' the hierarchy like so:
# lxsc.lua
local LXSC = {}
LXSC.SCXML = require 'lib/runtime'
return LXSC
# lib/runtime.lua
local SCXML = require 'lib/scxml'
function SCXML:go() ... end -- modify a table defined elsewhere
return SCXML
# lib/scxml.lua
local SCXML = {}
return SCXML
...but that technique does not work when multiple files augment the same datatype. Is there a pattern that allows multiple files to work on the same common table without polluting the global namespace?
3) One File Referencing Tables from Another
Various files and methods need to talk to other high-level objects in the system. For example:
# lib/parse.lua
function LXSC:parse(...) -- augments the master table
local name = getSomeNameString()
local object = LXSC[name]() -- dynamically picks methods from the master table
end
# lib/transition.lua
function Transition:addTarget(...)
self.targets = LXSC.List() -- accesses a datatype from the namespace
end
How would I cause the master table to be exposed to each child file that needs to access it?
Is setfenv() the proper way to go about all this? Is there something better?
On Mon, Feb 18, 2013 at 2:43 PM, Gavin Kistner
<phrogz@me.com> wrote: