Skip to content

Instantly share code, notes, and snippets.

@cprieto
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cprieto/9822843 to your computer and use it in GitHub Desktop.
Save cprieto/9822843 to your computer and use it in GitHub Desktop.
Sometimes closures are kind of different....
#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;
}
var banana = 1;
Func<int, int> func1 = x => { return x + banana; };
banana = 2;
var result = func1(3);
Console.WriteLine(result); // returns 5
banana = 1
func1 = lambda x: x + banana
banana = 2
result = func1(3)
print result # prints 5
#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;
}
<?
$banana = 1;
$greet = function($x) use ($banana) {
return $x + $banana; };
$banana = 2;
$result = $greet(3);
echo $result; // prints 4
#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;
}
#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;
}
<?
$banana = 1;
$greet = function($x) use (&$banana) {
return $x + $banana; };
$banana = 2;
$result = $greet(3);
echo $result; // prints 5
@aaronpowell
Copy link

JavaScript, ES6 style:

var banana = 1;

var fn = x => x + banana;

banana = 2;

console.log(fn(3));

@Hdesai
Copy link

Hdesai commented Mar 28, 2014

#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;
}

@aaronpowell
Copy link

@Hdesai you can use the backtick to set the code block so you get proper formatting :)

@Hdesai
Copy link

Hdesai commented Mar 28, 2014

@aaronpowell. Thanks! done.

@cprieto
Copy link
Author

cprieto commented Mar 28, 2014

@Hdesai

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).

@cprieto
Copy link
Author

cprieto commented Mar 28, 2014

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