Last active
August 29, 2015 13:57
-
-
Save cprieto/9822843 to your computer and use it in GitHub Desktop.
Sometimes closures are kind of different....
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdafx.h" | |
| #include <stdio.h> | |
| int _tmain(int argc, _TCHAR* argv[]) | |
| { | |
| auto banana = 1; | |
| auto func1 = [banana](int i) -> int { return i + banana; }; | |
| banana = 5; | |
| auto result = func1(3); | |
| printf("result is %d\n", result); // prints 4 | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var banana = 1; | |
| Func<int, int> func1 = x => { return x + banana; }; | |
| banana = 2; | |
| var result = func1(3); | |
| Console.WriteLine(result); // returns 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| banana = 1 | |
| func1 = lambda x: x + banana | |
| banana = 2 | |
| result = func1(3) | |
| print result # prints 5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| int main(int argc, const char *argv[]) { | |
| @autoreleasepool { | |
| int banana = 1; | |
| int (^func)(int) = ^int(int x) { | |
| return x + banana; | |
| }; | |
| banana = 2; | |
| int result = func(3); | |
| NSLog(@"%d", result); // prints 4 | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| $banana = 1; | |
| $greet = function($x) use ($banana) { | |
| return $x + $banana; }; | |
| $banana = 2; | |
| $result = $greet(3); | |
| echo $result; // prints 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdafx.h" | |
| #include <stdio.h> | |
| int _tmain(int argc, _TCHAR* argv[]) | |
| { | |
| auto banana = 1; | |
| auto func1 = [&banana](int i) -> int { return i + banana; }; | |
| banana = 5; | |
| auto result = func1(3); | |
| printf("result is %d\n", result); // prints 8!!! | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| int main(int argc, const char *argv[]) { | |
| @autoreleasepool { | |
| __block int banana = 1; | |
| int (^func)(int) = ^int(int x) { | |
| return x + banana; | |
| }; | |
| banana = 2; | |
| int result = func(3); | |
| NSLog(@"%d", result); // prints 5 | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| $banana = 1; | |
| $greet = function($x) use (&$banana) { | |
| return $x + $banana; }; | |
| $banana = 2; | |
| $result = $greet(3); | |
| echo $result; // prints 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh!
This is what your nice compiler generates:
See? now it's clear :)