Re: [Vala] collecting delegates



Thanks for the reply Jiří!

That seems to be an usable solution for this problem. 

I used vala-0.7.5 and git log doesn't look like it has been put into
master branch since then.
regards,
lariamat
 
That's a bug in Vala.
The problem is that delegate actually consists of two values - function 
pointer and object pointer. That's normally done by using two separate 
variables. Thus, for collections, you need to pack it into one value. 
Vala doesn't do it automatically at the moment I think (perhaps it's 
fixed in master, need to check).

You can work around it by doing this:

[Compact]
private class MyDelgTypeWrapper {
         public MyDelgType deleg;
         public MyDelgTypeWrapper (MyDelgType deleg) { this.deleg = deleg; }
}

...

list += new MyDelgTypeWrapper (this.dtest);

...

list[i].deleg(i+1, "hello");



On 08/09/2009 11:28 AM, lariamat wrote:
Dear List
I wanted to check the possibility to put delegates into an array or a
GLib.List and then firing them one by one in a foreach way.
After some attempts I found a compiling solution that unfortunately does
not work.
Can anybody tell me what I am doing wrong here?
Thanks
lariamat

public class DGLTest : GLib.Object {
    public DGLTest() {
            print("construct\n");
            list = {};
    }

    //delegatetype
    public delegate void MyDelgType(int aint, string bstr);
    
    //Array for collecting delegates
    private MyDelgType[] list;
    
    
    //delegate for testing
    public void dtest(int a, string b) {
            for(int i=0;i<a;i++)
                    print("%s-%d\n", b, i);
    }
    
    //test function for adding delegates to the array and calling them
afterwards
    public void run() {
            for(int i=0;i<4;i++) {
                    list+=(MyDelgType)this.dtest;
            }
            for(int i=0;i<list.length; i++) {
                    list[i](i+1, "hello");
            }
    }
    
    public static GLib.MainLoop loop;
    public static int main(string[] args) {
            loop = new GLib.MainLoop(null, false);
            DGLTest dglt = new DGLTest();
            dglt.run();
            loop.run();
            return 0;
    }
}
   
_______________________________________________
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]