|
Alternatively[1]:
On 19/04/16 08:45 AM, Luiz Henrique de Figueiredo wrote:
I see. This is the only one point i assume, was the wrong designIf you want this in your version of Lua, you could add a token filter
decision: Omit the explicit statement delimitter ";"
that replaces '; .' with '; self.' and the same for `:`.
See http://lua-users.org/lists/lua-l/2008-01/msg00524.html for a similar
but simpler solution to the same problem of avoiding writting 'self'.
local function with(t, e)
return setmetatable({}, {__index=function(t,k) return t[k] or e[k] end, __newindex = t})
end
do local _ENV = with(self, _ENV)
-- do stuff that relies on the internal environment here, all assignments are done on self and _ENV is used as a fallback.
f(x) -- basically self.f(self.x)
f(self) -- self is a local, so it doesn't pull from self
f(with) -- with is a local, so it doesn't pull from self
print("test") -- prints "test" unless `self.print` exists
-- etc
end
-- do stuff that relies on the external environment here
[1] - Inspired by http://lua-users.org/lists/lua-l/2016-03/msg00080.html
--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.