Sign up

How to handle switches

In the documentation, it has been discouraged to use addOnActivate() function and it also has been suggested that the users should use addOnNotify(). In the D forum 5 years ago, it has recommended that switch signals be handled as follows:

Switch sw = new Switch();
sw.addOnNotify(&notify);

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 -

  1. Is it still the latest way to connect activate signals to switches?
  2. 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
            }
        });

Re: How to handle switches

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 use addOnNotify(). In the D forum 5 years ago, it has recommended that switch signals be handled as follows:

Switch sw = new Switch();
sw.addOnNotify(&notify);

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 -

  1. Is it still the latest way to connect activate signals to switches?
  2. 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".