Hacker News new | ask | show | jobs
by wingspan 4733 days ago
The C# compiler does the same thing:

http://blogs.msdn.com/b/ericlippert/archive/2007/06/06/fyi-c...

This is an old article, but I believe it still applies to the latest iteration of the language.

1 comments

Does indeed still apply, and is actually formalized into the language spec itself [1] in section 7.15.5.1 Captured outer variables. So it's actually a feature (really) in the case of C#, not a bug.

[1] http://www.microsoft.com/en-us/download/details.aspx?id=7029

"the local variable x is captured by the anonymous function, and the lifetime of x is extended at least until the delegate returned from F becomes eligible for garbage collection (which doesn’t happen until the very end of the program)."

I don't see how that part of standard mandates that all anomymous functions share the same closure. The example only has a single lambda.