diff --git a/README.md b/README.md index e961d6f..ddd29c8 100644 --- a/README.md +++ b/README.md @@ -172,20 +172,21 @@ func scope() func() int{ } func another_scope() func() int{ + // won't compile because outer_var and foo not defined in this scope outer_var = 444 - return foo // foo() will return 2 + return foo } // Closures: don't mutate outer vars, instead redefine them! -func outer() func() int, int{ +func outer() (func() int, int) { outer_var := 2 inner := func() int { outer_var += 99 // attempt to mutate outer_var from outer scope return outer_var // => 101 (but outer_var is a newly redefined // variable visible only inside inner) } - return foo, outer_var // => 101, 2 (outer_var is still 2, not mutated by foo!) + return inner, outer_var // => 101, 2 (outer_var is still 2, not mutated by foo!) } ```