[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: from: Lua Module Function Critiqued
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 7 Oct 2013 18:51:31 +0200
2013/10/7 Andrew Starks <andrew.starks@trms.com>:
> PS: (function print("hello") end )() is a good way to encapsulate
> something within a scope, but it ends up leading to a bunch of ";" and
> is more verbose than using "do ... end".
>
> do
> print("hello")
> end
The two techniques are not equivalent.
A do..end block limits the scope of the local variables, true,
but there must be enough slots left to accommodate them. Whereas
immediate execution of an anonymous function allows you to start
from scratch: locals outside become upvalues.
You can demonstrate the difference thus:
Make 20 copies of a line with 10 copies of 'local x ' on it.
After that,
do local y print(x==y) end
gives a 'too many local variables' error, whereas
(function () local y print(x==y) end)()
works.