www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Dlang] Delegate Syntax Question

reply Jack <Jackoz530 gmail.com> writes:
Hello. So I was trying to pass a delegate as an argument in a 
function and was wondering if I'm writing the correct code for it.

You see my code is :


//////////////////////////////////////////


class Foo()
{
  void bar()
  {
  writeln("Hello World");
  }
}

class Bar()
{

void delegate() action;

   void setAction(void delegate() dele)
   {
    action = dele;
   }

}

void main()
{
Foo foo = new Foo();
Bar bar = new Bar();
bar.setAction(&foo.bar);
bar.action();
}

/////////////////////////////////////////

Is this correct? Because I've been having trouble calling the 
delegate when passing the method and I read many documentation 
concerning function and delegates. I'm just confused. 
(Disclaimer: My code's pattern is the same as above but it's not 
really my exact code)
Jan 10 2016
next sibling parent anonymous <anonymous example.com> writes:
On 10.01.2016 15:32, Jack wrote:
 //////////////////////////////////////////


 class Foo()
Those parentheses make this a (zero parameter) class template. I suppose you just want a plain class. Drop them then.
 {
   void bar()
   {
   writeln("Hello World");
   }
 }

 class Bar()
ditto
 {

 void delegate() action;

    void setAction(void delegate() dele)
    {
     action = dele;
    }

 }

 void main()
 {
 Foo foo = new Foo();
 Bar bar = new Bar();
 bar.setAction(&foo.bar);
 bar.action();
 }

 /////////////////////////////////////////

 Is this correct? Because I've been having trouble calling the delegate
 when passing the method and I read many documentation concerning
 function and delegates. I'm just confused. (Disclaimer: My code's
 pattern is the same as above but it's not really my exact code)
Aside from the mentioned parentheses (and a missing import), everything's correct.
Jan 10 2016
prev sibling next sibling parent Gary Willoughby <dev nomad.so> writes:
On Sunday, 10 January 2016 at 14:32:02 UTC, Jack wrote:
 ...
Just to make your code a little more clear, try using aliases when defining delegate parameters. Like this: alias Action = void delegate(); Then in your code you use the alias, like this: class Bar() { private Action _action; void setAction(Action d) { this._action = d; } } IMHO it makes everything more readable and you only have one definition of the delegate signature.
Jan 10 2016
prev sibling parent Jack <Jackoz530 gmail.com> writes:
On Sunday, 10 January 2016 at 14:32:02 UTC, Jack wrote:
 Hello. So I was trying to pass a delegate as an argument in a 
 function and was wondering if I'm writing the correct code for 
 it.

 You see my code is :


 //////////////////////////////////////////


 class Foo()
 {
  void bar()
  {
  writeln("Hello World");
  }
 }

 class Bar()
 {

 void delegate() action;

   void setAction(void delegate() dele)
   {
    action = dele;
   }

 }

 void main()
 {
 Foo foo = new Foo();
 Bar bar = new Bar();
 bar.setAction(&foo.bar);
 bar.action();
 }

 /////////////////////////////////////////

 Is this correct? Because I've been having trouble calling the 
 delegate when passing the method and I read many documentation 
 concerning function and delegates. I'm just confused. 
 (Disclaimer: My code's pattern is the same as above but it's 
 not really my exact code)
Replying to my self because I don't know how I can reply to two posts at once. Thank you for helping. I've had a problem on handling delegates in my code so I was not sure if it was working. I'll post here if I can't find the solution.
Jan 12 2016