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



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]