After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 672767 - Local variable from foreach block does not get assigned for closure
Local variable from foreach block does not get assigned for closure
Status: RESOLVED DUPLICATE of bug 628336
Product: vala
Classification: Core
Component: Code Generator
0.14.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2012-03-24 20:23 UTC by lzap
Modified: 2012-03-24 20:48 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description lzap 2012-03-24 20:23:55 UTC
Hello,

in this code:

class ClosureBug: GLib.Object {
  class TestClass {
    public int test = 42;
  }

  delegate int ReturnTestClass();

  TestClass[] tests = new TestClass[2];

  public void do_test() {
    tests[0] = new TestClass();
    tests[1] = new TestClass();

    ReturnTestClass rtc = null;
    foreach (TestClass tc in tests) {
      rtc = () => { return tc.test; };
    }

    GLib.stdout.printf("This should be 42: %d\n", rtc());
  }

  public static int main(string[] args) {
    ClosureBug cb = new ClosureBug();
    cb.do_test();
    return 0;
  }
}

Vala generates an error: 
/tmp/closure-test1.vala.PH3CBW.c: In function ‘closure_bug_do_test’:
/tmp/closure-test1.vala.PH3CBW.c:198:4: error: ‘tc’ undeclared (first use in this function)

It seems the generator prepares the closure structure correctly (from the generated code):

struct _Block1Data {
	int _ref_count_;
	ClosureBug * self;
	ClosureBugTestClass* tc;
};

But for some reason it cannot use it.
Comment 1 Luca Bruno 2012-03-24 20:30:59 UTC

*** This bug has been marked as a duplicate of bug 628336 ***
Comment 2 lzap 2012-03-24 20:47:11 UTC
Thanks Luca. The workaround is to use Gee arrays.