closures

How golang's “defer” capture closure's parameter?

纵饮孤独 提交于 2019-12-03 02:40:44
问题 Here is my code (run): package main import "fmt" func main() { var whatever [5]struct{} for i := range whatever { fmt.Println(i) } // part 1 for i := range whatever { defer func() { fmt.Println(i) }() } // part 2 for i := range whatever { defer func(n int) { fmt.Println(n) }(i) } // part 3 } Output: 0 1 2 3 4 4 3 2 1 0 4 4 4 4 4 Question: What's the difference between part 2 & part 3? Why part 2 output "44444" instead of "43210"? 回答1: The 'part 2' closure captures the variable 'i'. When the

Why can an anonymous class access non-final class member of the enclosing class

南笙酒味 提交于 2019-12-03 02:12:31
We know that only final local variables can be accessed in an anonymous class, and there is a good reason here: Why are only final variables accessible in anonymous class? . However, I found that an anonymous class can still access a non-final variable if the variable is an member field of the enclosing class: How can I access enclosing class instance variables from inside the anonymous class? I am confused. We ensure that only a final local variable can be accessed in anonymous class because we don't want that the variable should be out-of-sync between anonymous class and local function. The

What does “to close over the enclosing scope/class” mean?

夙愿已清 提交于 2019-12-03 02:11:29
The Akka documentation is documenting dangerous variants of using Props : // NOT RECOMMENDED within another actor: // encourages to close over enclosing class val props7 = Props(new MyActor) Then carries on stating: This method is not recommended to be used within another actor because it encourages to close over the enclosing scope, resulting in non-serializable Props and possibly race conditions (breaking the actor encapsulation). Could someone please explain the meaning of "closing over the enclosing scope"? Been looking all over and found nothing. Thanks. It's a bit tricky to see in this

Please explain this Javascript closure exercise [duplicate]

点点圈 提交于 2019-12-03 01:17:04
This question already has answers here : How do JavaScript closures work? (86 answers) I'm a javascript noob trying to wrap my head around the closure exercise below. Now, I know the result is 122. Can anyone walk me through this step by step (what gets passed to what), so I can understand how closures work? var hidden = mystery(3); var jumble = mystery3(hidden); var result = jumble(2); function mystery ( input ){ var secret = 4; input+=2; function mystery2 ( multiplier ) { multiplier *= input; return secret * multiplier; } return mystery2; } function mystery3 ( param ){ function mystery4 (

What is the difference between $.proxy() and bind()?

不羁岁月 提交于 2019-12-03 01:14:55
In 2009, ECMAScript 5 added a built-in bind() function which takes an object as a parameter and returns an identical function in which this will always refer to the object you passed it. (I couldn't find anything that looked like a canonical documentation link.) How is this different from jQuery's $.proxy() function ? Did $.proxy() come first before ECMAScript 5 was released? Is there a particular reason to favor $.proxy(function(){}, this) over function(){}.bind(this) ? Matt Whipple proxy came first and you should likely favor bind as it is a standard. The way they are called varies slightly

Variable captured by closure before being initialized

旧时模样 提交于 2019-12-03 01:03:44
I'm trying to store the number of results from a query into an integer so that I can use it to determine the number of rows in a table. However, I'm getting the following error: Variable 'numberOfGames' captured by a closure before being initialized' on the line query.findObjectsInBackgroundWithBlock{ . I also get another error Variable 'numberOfGames' used before being initialized on the line return numberOfGames . Here's the function that contains the two errors: func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { var user: PFUser! var numberOfGames: Int //..

groovy: safely find a key in a map and return its value

不想你离开。 提交于 2019-12-03 00:56:51
I want to find a specific key in a given map. If the key is found, I then want to get the value of that key from the map. This is what I managed so far: def mymap = [name:"Gromit", likes:"cheese", id:1234] def x = mymap.find{ it.key == "likes" } if(x) println x.value This works, the output is "cheese" as expected. Great, but I don't want to do x.value at the end, and I don't want to do if(x) . I want x to directly contain the value somehow. I can't get the value directly into x like this: def mymap = [name:"Gromit", likes:"cheese", id:1234] def x = mymap.find{ it.key == "likesZZZ" }.value

Are there any good reasons why closures aren't immutable in C#?

久未见 提交于 2019-12-03 00:35:31
I've been going over and over this in my head, and I can't seem to come up with a good reason why C# closures are mutable. It just seems like a good way to get some unintended consequences if you aren't aware of exactly what's happening. Maybe someone who is a little more knowledgeable can shed some light on why the designers of C# would allow state to change in a closure? Example: var foo = "hello"; Action bar = () => Console.WriteLine(foo); bar(); foo = "goodbye"; bar(); This will print "hello" for the first call, but the outside state changes for the second call, printing "goodbye." The

Please explain closures, or binding the loop counter to the function scope

老子叫甜甜 提交于 2019-12-03 00:31:12
I've seen programmers assign events listeners inside loops, using the counter. I believe this is the syntax: for(var i=0; i < someArray.length; i++){ someArray[i].onclick = (function(i){/* Some code using i */})(i); } Could someone please explain the logic behind this, and this weird syntax, I've never seen this: (function(i))(i); Many thanks for your time and patience. This is done because JavaScript only has function scope , not block scope. Hence, every variable you declare in a loop is in the function's scope and every closure you create has access to the very same variable. So the only

Understanding closure in Javascript

我怕爱的太早我们不能终老 提交于 2019-12-03 00:26:35
I'm trying to wrap my head around closures in Javascript. Here is an example from a tutorial: function greeter(name, age) { var message = name + ", who is " + age + " years old, says hi!"; return function greet() { console.log(message); }; } // Generate the closure var bobGreeter = greeter("Bob", 47); // Use the closure bobGreeter(); The author said that this is an effective way of using closure to make private variables, but I don't get the point. Could someone enlighten the benefits of coding like this? A closure is a pair of a function and the environment in which it was defined (assuming