lua-users home
lua-l archive

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


On Dec 6, 2010, at 6:32 AM, steve donovan wrote:

> Any powerful language allows bad coding practices. So style and
> consistent idiom use is important.  In this case the rule could be:
> "avoid idioms where local constructs require global changes".
> 
> Such shortcuts don't need global changes, could define
> 
> local function at(s,i) return s:sub(i,i) end
> 
> and you would not be typing many more characters, plus your readers
> will see the explicit function call and be grateful.

at(s,i) would probably also execute faster than s[i] given that the latter has to invoke the __index metamethod and then figure out what use case was meant assuming that one preserves the ability to invoke string functions via colon notation. Of course, if speed is of the essence, one wouldn't be using the colon notation either since it requires a table lookup on every use.

Mark