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 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 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 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 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 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 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 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 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 |
#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;
}
@Hdesai you can use the backtick to set the code block so you get proper formatting :)
@aaronpowell. Thanks! done.
If you take a look it actually returns 8 and not 4, why? because "banana" is capture by reference and not by copy (the default behaviour).
Oh!
This is what your nice compiler generates:
using System;
using System.Runtime.CompilerServices;
namespace ConsoleApplication2
{
internal class Program
{
public Program()
{
base.\u002Ector();
}
private static void Main(string[] args)
{
Program.\u003C\u003Ec__DisplayClass1 cDisplayClass1 = new Program.\u003C\u003Ec__DisplayClass1();
cDisplayClass1.banana = 1;
// ISSUE: method pointer
Func<int, int> func = new Func<int, int>((object) cDisplayClass1, __methodptr(\u003CMain\u003Eb__0));
cDisplayClass1.banana = 2;
Console.WriteLine(func(3));
}
[CompilerGenerated]
private sealed class \u003C\u003Ec__DisplayClass1
{
public int banana;
public \u003C\u003Ec__DisplayClass1()
{
base.\u002Ector();
}
public int \u003CMain\u003Eb__0(int x)
{
return x + this.banana;
}
}
}
}
See? now it's clear :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JavaScript, ES6 style: