[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Require
- From: Mark Hamburg <mhamburg@...>
- Date: Sun, 25 Jul 2004 10:33:44 -0700
Yes, export puts values in the dictionary used by import.
I thought about making import just use the __index metamethod but was left
feeling fuzzy on what to do in the event of an error. If it really acts just
like a table, then it should just return nil if the value isn't present.
True an __index metamethod can throw an exception, but that's a departure
from what the syntax would lead one to expect.
I like your implementation. It's a bit simpler than mine. On the other hand,
the complexity in mine may be from the fact that I take explicit steps to
detect and disallow cyclic import.
I've been thinking of extending my import function to allow specifying a
series of names to extract from the exported value:
local _, addObserver, removeObserver, notifyObservers =
import( "com.baymoon.Observations", "add", "remove", "notify" )
Missing names would trigger an exception at import time.
Mark
on 7/24/04 5:56 PM, Rici Lake at lua@ricilake.net wrote:
> That is very pretty.
>
> I assume when you say "exports one or more named values" that you mean
> that it exports them to the dictionary import() uses? In that
> case, you could even make import the dictionary, instead of a
> function, and define an __index metamethod.
>
> That would make the implementation something like this, leaving out
> the interesting parts (like the bundle-finder and the bundle-runner):
>
> import = setmetatable({}, {__index =
> function(self, name)
> local bundle, bundlename = findBundleForName(name)
> setfenv(bundle, setmetatable({}, {__index = getfenv(0)})
> local exports = bundle()
> for k, v in exports do
> if rawget(self, k) == nil then self[k] = v end
> end
> local rv = rawget(self, name)
> if rv == nil then
> error(bundlename .. " did not export " .. name)
> end
> return rv
> end})
>
> -------
>
> local showRect = import.showRect