Re: [Vala] Passing user_data in signals is supported, but could be better.




I know this method, and I'm happy it's possible.
And no, I don't have problem at all while coding, I just think that Vala
shell support also other syntax.
I just wish Vala could use some thing like this:
another_object.my_signal.connect(user_data:my_method);
I wasn't a real C developer in my past, but I think they would agree that
Vala code generator shell call callbacks directly when possible, not via
the generated lambda function.
I know that there won't be an extra call at run time, if my_method be an
internal method.
I just think that if it possible to do it in other language, C practically, and it's not
such a bad idea, doesn't conflict with Vala mechanics, so why not?
Again, it's just a feature request, and I ask from developers and users to tell
me if it would be great to have it in Vala.
So what do you think?

From: alexandre rosenfeld gmail com
Date: Mon, 10 Oct 2011 14:32:15 -0300
Subject: Re: [Vala] Passing user_data in signals is supported, but could be better.
To: tal_hd hotmail com
CC: vala-list gnome org

I understand your problem, but you can still use lambdas. If you try to fool the compiler into taking a 
pointer as a user data, you end up with reference issues (what happens if no one uses the object but the 
signal handler?). The easiest solution is to use lambdas, so that Vala manages the references to objects for 
you automatically. And from the lambda you can call another method.


For instance, check this non-working, out-of-my-head code:

class A : Object {
    void my_method(int b) {
        printf("Got %d\n", b);
    }
}

int main(string[] args) {
    a = new A();


    int i = 5;
    another_object.my_signal.connect(() => {
        a.my_method(i);
    });
}

If you check the generated C code, you´ll see Vala created a reference-counted struct to hold a and i. This 
struct is automatically freed for you and it holds the a reference, so that when you free a in the main, the 
signal will keep working since it holds a reference to the data struct, which holds a reference to a.


If you just use pointers to try and fool the compiler into accepting an object as user_data, you end up 
having to do this management yourself and many times it´s actually impossible to do it right.
Alexandre Rosenfeld



2011/10/10 Tal Hadad <tal_hd hotmail com>




Thanks for your answer Alexandre Rosenfeld, but you get me wrong.



Why not just use lambda? From the Gtk+ example:

Yes I know I can do this. I point this out in my post:

2. Variables outside the lambda expression.



Some prefer using lambda, some prefer using traditional methods.

Another quote I said:

Method 2 is done perfectly by Vala, when we talk about lambda expression.



Some(I'm included) prefer to use traditional methods than lambda.

It is easier to read, document, etc.

Don't you think it's a good idea to allow passing user_data directly

using traditional methods?

The last sentence is the whole point of my post.



From: alexandre rosenfeld gmail com

Date: Mon, 10 Oct 2011 12:16:13 -0300

Subject: Re: [Vala] Passing user_data in signals is supported, but could be better.

To: tal_hd hotmail com

CC: vala-list gnome org



Why not just use lambda? From the Gtk+ example:

    var button = new Button.with_label ("Click me!");





    button.clicked.connect (() => {





        button.label = "Thank you";





        this.my_data = 3;

    });

And you can have that my_data be anything you want, an instance member or a property in any other instance 
(doesnt have to be from this).





Alexandre Rosenfeld



2011/10/10 Tal Hadad <tal_hd hotmail com>









Vala does support passing a "context" argument, calling user_data to callbacks,



by this two methods(source: FAQ and Tutorial):



1. Passing "this"/self as user_data, for instance method.



2. Variables outside the lambda expression.



Method 2 is done perfectly by Vala, when we talk about lambda expression.



But method 1 is not enough... Not always it's "this"/self what that needed to be



passed.



By now, two ways to overcome this, without using lambda:



* The dirty trick:



instance.event_name.connect (((instance_type*)user_data)->method);



...



private void method ([args]) {



    user_data_type* user_data = this;



    // Perform action with user_data.



}







* Back to basic(using GLib.Signal namespace):



Signal.connect (instance, "event_name", (GLib.Callback)method, user_data);



...







private static void static_on_stop_clicked ([args], user_data_type user_data) {







    // Perform action with user_data.







}







Both works, but I think a proper syntax should be.



I really like the instance.method way for passing callbacks, but it's valid only for



"this"/self. So, perhaps, instance:method for static methods?



What do you think? Shell Vala support this feature?



Tal





_______________________________________________



vala-list mailing list



vala-list gnome org



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







                                          
_______________________________________________

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]