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



Why not just use lambda? From the Gtk+
example<http://live.gnome.org/Vala/GTKSample>
:

    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]