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




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
                                          


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