lua-users home
lua-l archive

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


On Tue, Oct 5, 2010 at 4:05 PM, Mark Hamburg <mark@grubmah.com> wrote:
>> Better to not muck with environments at all. The following works just fine
>> in Lua 5.1 and 5.2:
>>
>> local M = { }
(...)
>> function M.exportedFunction2()
>> -- code
>> end
>> local exportedFunction2 = M.exportedFunction2 -- fast local access to the
>> function
>> local function privateFunction()
>> -- code
>> end
>> function M.exportedFunction3()
>> privateFunction()
>> exportedFunction2()
>> end
>> return M
>

(...)

> I guess that's not so bad, if you just accept to write the prefixes. What's
> so bad about mucking with environment if you consider the example Steve
> gave? I don't see any downsides, besides that prefixes make the code a bit
> more explicit (but also a little bit less readable). Is there a performance
> issue maybe?
>

Why not a "syntactic sugar" like:

  "local M.exportedFunction1 = function() end" or "local
M.exportedFunction1 () end"

meaning

  "local exportedFunction1 = function() end;   M.exportedFunction1  =
exportedFunction1" or vice-versa?

And finally,  " return M "

That would allow you to declare the function name only once but would
create automatically a local function variable for a faster data
access and also would reduce the typing effort of variables names and
of the return statement.

-- 
Nilson