lua-users home
lua-l archive

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

Since starting to play around with lua 5.2, I've realized that two of my favorite patches are both one liners.  I'm tempted to post them on the wiki, but, I'm enough of a lua novice that I'm a little worried that they might have unintended side effects.  So maybe I should solicit some feedback from you guys first?

The first one-liner is Brian Palmer's suggestion of making the "then" token in if-then statements optional, by changing the checknext() in lparser:test_then_block() to a testnext().  I think this is a really nice change, enough so that it's probably worth documenting independently of the concise anonymous functions patch.  I'm a little confused about why testnext() isn't the default behavior though -- I'm guessing there are some cases where leaving out a "then" can lead to parse errors?

The second is switching the argument to lparser:body()'s new_localvarliteral() call from "self" to LUA_ENV, thus changing the ":" function declaration sugar from

function a:b(...) --> a.b = function (self,...)
function a:b(...) --> a.b = function(_ENV,...)

This is a breaking change, in the sense that it has good odds of profoundly changing the semantics of vanilla lua code.  It also leads to much more concise object definitions, implying that most of the time, there's no longer any need to prefix member variables with self.  As someone with a C++ background, the need to do explicit self-prefixing in lua has always bothered me, and getting rid of it makes the language feel much "cleaner".  So it seems like a patch that might be worth documenting, even though it flunks the backwards compatibility test.


PS: I'm a little suspicious that one of the reasons that LUA_ENV is exposed in lua_conf.h is to make it easy to switch the environment name from "_ENV" to "self", which has about the same effect as my patch.  Does anyone know if this was actually the intention of Roberto et al., and if so, why they preferred it to making the opposite substitution?