[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release)
- From: David Manura <dm.lua@...>
- Date: Wed, 6 May 2009 00:44:38 -0400
On Tue, May 5, 2009 at 9:47 PM, David Manura wrote:
> I think it would be ideal if we could inject only lexically (the
> injection limited to the lexical scope)...
> modify the string metatable to be scope aware...
A cleaner solution to that can be achieved in Metalua. The Metalua
solution has been added to the MethodChainingWrapper page.
A test example of it goes like this:
-{extension "lexicalindex"}
-- test example libraries
local stringx = {}
function stringx.trim(self) return self:match('^%s*(%S*)%s*$') end
local function f(o,k)
if type(o) == 'string' then return stringx[k] or string[k] end
return o[k]
end
local function test(s)
assert(s.trim == nil)
lexicalindex f
assert(s.trim ~= nil)
assert(s:trim():upper() == 'TEST')
end
local s = ' test '
assert(s.trim == nil)
test(s)
assert(s.trim == nil)
print 'DONE'
The above gets transformed by Metalua into this corresponding plain Lua code:
--- $ ./build/bin/metalua -S vs.lua
--- Source From "@vs.lua": ---
local function __li_invoke (__li_index, o, name, ...)
return __li_index (o, name) (o, ...)
end
local stringx = { }
function stringx:trim ()
return self:match "^%s*(%S*)%s*$"
end
local function f (o, k)
if type (o) == "string" then
return stringx[k] or string[k]
end
return o[k]
end
local function test (s)
assert (s.trim == nil)
local __li_index = f
assert (__li_index (s, "trim") ~= nil)
assert (__li_invoke (__li_index, __li_invoke (__li_index, s,
"trim"), "upper") == "TEST")
end
local s = " test "
assert (s.trim == nil)
test (s)
assert (s.trim == nil)
As seen, the index (including method call) operations inside the
current scope are transformed into function call operations specified
by the user.
[1] http://lua-users.org/wiki/MethodChainingWrapper
- References:
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), steve donovan
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), Henk Boom
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), Philippe Lhoste
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), David Manura
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), Henk Boom
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), Petite Abeille
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), steve donovan
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), Petite Abeille
- Re: Injecting names & method chaining (was Re: [ANN] Penlight Libraries, First release), David Manura