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




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



                                          


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