Difference between revisions of "Closing over Variables"

From JVMLangSummit
Jump to navigationJump to search
(New page: == 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...)
 
 
Line 1: Line 1:
== Closing over variables considered harmful
+
== Closing over variables considered harmful ==
  
 
http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx
 
http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx
  
 
Agreeing with the author:
 
Agreeing with the author:
John Rose 22 Jan 2010 8:56 PM
+
<blockquote>
@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."
+
John Rose 22 Jan 2010 8:56 PM
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.
 
  
 +
@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."
  
== Closing over variables considered beneficial
+
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.
 +
</blockquote>
 +
 
 +
== Closing over variables considered beneficial ==
  
 
From your internal interator body, you can manage loop accumulators as if by a primitive while loop.
 
From your internal interator body, you can manage loop accumulators as if by a primitive while loop.

Latest revision as of 11:36, 20 July 2011

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.