[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: [proposal] A statement that can return value (and can used in expr)
- From: Xavier Wang <weasley.wx@...>
- Date: Wed, 14 Dec 2011 04:24:49 +0800
2011/12/14 Luiz Henrique de Figueiredo <email@example.com>:
>> local a = (function(a, b) print "a statement" return a + b end)(10, 20) + 30
> Lua 5.1 will create a new closure every time this statement is execute.
> Lua 5.2 will not: it will resuse the closure.
> Try the code below in each version:
> for i=1,3 do
> local f = function(a, b) print "a statement" return a + b end
> local a = f(10, 20) + 30 + i
Thank you for fast reply :-)
the promise only apply when you don't use upvalue, try this:
for i = 1, 3 do
local c = 10
local function f(a, b) print "a statement" return a + b + c end
local a = f(10 + 20) + 30
In this case, three closure will created, but they are needn't create
twice (they only execute once).
So, Is there any optimizing to make sure if a function only execute
once, there are needn't create once? or a new syntax form can hold
thanks Luiz :-)