lua-users home
lua-l archive

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


On Jul 13, 2010, at 9:55 AM, GrayFace wrote:

> The optimization is very strange. When is it really needed? I mean, moving function definitions outside the defining function here and there isn't a problem, unless there's some special case when this happens frequently.

The optimization is interesting because inline closure creation can make code flow clearer. Without it, one needs to come up with a name for what would otherwise be an anonymous function and put it out of line with where it will actually be used. But inline closures also come at the price of memory allocation, initialization, and collection churn. The optimization avoids that churn.

	seq:map( function( x ) return x * x end )

v

	local function square( x ) return x * x end
	
	-- code
	-- code
	-- code

	seq:map( square )

Now, in this case we could come up with a reasonable name, but there are plenty of cases where there is a blindingly obvious name and the function has to be defined away from where it is actually used.

Finally, though, as a counter argument, I will admit that there are probably plenty of cases where the need to structure the closures to avoid up value dependencies -- i.e., to keep them from closing over too much that can change -- probably undercuts the clarity.

Mark