On 10/11/18 1:02 PM, aedt wrote:
In the documentation, it has been discouraged to use
addOnActivate()
function and it also has been suggested that the users should useaddOnNotify()
. In the D forum 5 years ago, it has recommended that switch signals be handled as follows:Switch sw = new Switch(); sw.addOnNotify(¬ify); void notify(ParamSpec param, ObjectG obj) { if(param.getName() == "active") { //Do stuff } }
I am using this Switches like this:
auto sw = new Switch(); sw.addOnNotify(delegate void(ParamSpec p, ObjectG _y) { if (p.getName() == "active") { static bool on; on = sw.getActive(); //do stuf import std.stdio : writeln; writeln(on ? "On" : "Off"); } });
I have two questions -
- Is it still the latest way to connect activate signals to switches?
- How is it different that the code below:
auto sw = new Switch(); sw.addOnNotify(delegate void(ParamSpec _x, ObjectG _y) { if (sw.getActive()) { // Do stuff } });
I would probably do something like this:
auto sw = new Switch();
sw.addOnNotify(delegate void(_, __) {
if (sw.getActive()) {
// Do stuff
}
}, "active");
When using inline delegates / lambadas the so we don't have to specify
the types of the parameters.
Using _
and __
as the parameter names since we don't use them.
With the "active"
at the end the delegate is only called when the active
property of the switch changes.
Whether to use a inline delegate / lambada or a separate function that
will be passed in is personal preference. Personally i would base my
decision on the size of: "// Do stuff".