lua-users home
lua-l archive

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


  Swap "_ENV" for self though, and suddenly lua can go that extra 10% of the way towards supporting a C++ style implicit self.

Well, maybe I'm overstating things a bit here.  I mean, even if you use the pattern

function a.f(_ENV,..)

You still need to explicitly reference self/_ENV anytime you call another member function.  So, maybe this is one good reason to prefer defining LUA_ENV to "self", rather than using my patch.

Nested member function calls like these look a lot cleaner if we prefix them with self, rather than _ENV:

function a:f()
    self:b()
    self:c()
end

On the other hand, it seems easy enough to add an extra little piece of sugar to the parser, so that if we encounter an unprefixed ':' where we expect an _expression_, we replace it with '_ENV : '.  i.e., add a case statement like this to lparser:prefixexp:

case ':': {
   singlevaraux(ls->fs, ls->envn, v, 1);
   return;
}

So now we can can get member function calls that are about as terse as C++'s,

function a:f()
    :b()
    :c()
end

Though maybe I'm taking the quest for implicit self semantics a little far at this point...