[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: local "module" pattern without module [was Re: [ANN] SLAXML - pure Lua, robust-ish, SAX-like streaming XML processor]
- From: Gavin Kistner <phrogz@...>
- Date: Mon, 18 Feb 2013 23:12:22 -0700
On Feb 18, 2013, at 11:02 PM, steve donovan <steve.j.donovan@gmail.com> wrote:
> One consequence of setfenv magic is that everything becomes slower,
> because you now have indirect lookup for everything (that's another
> criticisim of module(...,package.seeall) apart from allowing all your
> laundry to be exposed).
Understood. I have benchmarking in place that shows no measurable performance degradation for my usage, though I do cache certain library functions in locals anyhow already.
> I've found myself doing things like this to split a module over
> several files (useful if there's extra functionality you might want to
> load)
>
> --foo.lua
> local foo = {}
>
> function foo.answer() ... end
> ....
> return foo
>
> --foo.extra.lua
> local foo = require 'foo'
>
> function foo.more() ... end
>
> end
>
> return foo
This pattern is dandy. I use it myself in SLAXML:
--> slaxml.lua <--
local SLAXML = { ... }
...
return SLAXML
--> slaxdom.lua <--
local SLAXML = require 'slaxml'
function SLAXML:dom(xml,opts) ... end
return SLAXML
This way the user can require just the core SAX parser or the DOM (which uses the SAX).
However, unless I'm missing something, this only works with strict (inverted) hierarchies of dependencies. It falls down if you two files that both extend the same base file and the user should be able to get code from both.
- References:
- Re: local "module" pattern without module [was Re: [ANN] SLAXML - pure Lua, robust-ish, SAX-like streaming XML processor], Gavin Kistner
- Re: local "module" pattern without module [was Re: [ANN] SLAXML - pure Lua, robust-ish, SAX-like streaming XML processor], Petite Abeille
- Re: local "module" pattern without module [was Re: [ANN] SLAXML - pure Lua, robust-ish, SAX-like streaming XML processor], Gavin Kistner
- Re: local "module" pattern without module [was Re: [ANN] SLAXML - pure Lua, robust-ish, SAX-like streaming XML processor], steve donovan