[Vala] Idle.add lambda function with local variable



Hi,

I'm trying the following in Vala:


public class Main : Object {

  public static int main (string[] args) {

    int x = 3;

    Idle.add ( () => {
      print ("%d\n", x);
      x = 4;
      return false;
    });

    for (int i = 0; i < 10; i++) {
      Thread.usleep (100000);
    }
    print ("%d\n", x);

    return 0;
  }
}


It compiles, but with a warning:


/media/Data/Code/Vala/valatest/lambda/main.vala.c: In function ‘main_main’:
/media/Data/Code/Vala/valatest/lambda/main.vala.c:87: warning: passing argument 4 of ‘g_idle_add_full’ from 
incompatible pointer type
/usr/include/glib-2.0/glib/gmain.h:291: note: expected ‘GDestroyNotify’ but argument is of type ‘void 
(*)(struct Block1Data *)’


And when I run it, it only prints "3", when I expect it to print "3" and then "4".

My question: is it possible to access a local variable in a lambda function like this? Or should I use a 
different method to pass a local variable to a function I want to add to Idle?

(The reason I need to add something to Idle is because in my project I use Gtk and threads, and you shouldn't 
interact with Gtk elements from another thread.)







[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]