lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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 design
decision: Omit the explicit statement delimitter ";"
If you want this in your version of Lua, you could add a token filter
that replaces '; .' with '; self.' and the same for `:`.

See 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})

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
-- do stuff that relies on the external environment here

[1] - Inspired by

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.