Closing over Variables

From JVMLangSummit
Revision as of 11:36, 20 July 2011 by Jrose (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Closing over variables considered harmful

http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx

Agreeing with the author:

John Rose 22 Jan 2010 8:56 PM

@Konrad: "I much prefer Java’s rule here: only allow closing over variables which are declared `final`. But then, this only works because Java allows local variables to be declared `final`, which C# doesn’t."

The reason Java supports final variables is exactly to fix this problem. The designer of inner classes was a Lisp refugee who had been bitten by the DOTIMES issue (see Peter Seibel above), and didn't want to allow the problem into Java.

This particular barn door is hard to close after the mutable iteration variable has galloped away.

Closing over variables considered beneficial

From your internal interator body, you can manage loop accumulators as if by a primitive while loop.