Re: [Vala] Idle.add lambda function with local variable



Sorry, forgot to mention I'm using Vala 0.7.9 from the Ubuntu PPA.

Also I just realized my example may not be the best, because in it I use an int instead of an object as I do 
in my real project. But anyway, my question still stands.


--- On Sun, 12/27/09, Geert Jan <gj_alsem yahoo com> wrote:

From: Geert Jan <gj_alsem yahoo com>
Subject: [Vala] Idle.add lambda function with local variable
To: vala-list gnome org
Date: Sunday, December 27, 2009, 5:16 PM
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.)



      
_______________________________________________
Vala-list mailing list
Vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list







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