From 833b6cc739878d84a23f1c38ba083dfb087b18ad Mon Sep 17 00:00:00 2001 From: LordLandon Date: Sun, 3 May 2015 16:49:38 -0700 Subject: [PATCH] Update README.md Making scope example explicit, making closure example compilable --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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!) } ```