Helper objects to create delegates

Jason House jason.james.house at gmail.com
Wed May 30 17:44:44 PDT 2007


I have a multi-threaded application where I pass delegates between 
threads.  While I thought this was a good design in the beginning, I 
find myself having to create a lot of helper objects...  Is there any 
way to avoid all the extra typing?  Any candidate additions to the 
language that'd help? :)

The following results in a run time error:
void example(){
   foreach(foo x; objectList)
     passDelegate({x.func(7);});
}

The following works but is annoying to keep typing:

class Helper{
   foo x;
   int param;
   this(foo _x, int _param){
     x     = _x;
     param = _param;
   }
   void opCall(){x.func(param);}
}
void example(){
   foreach(foo x; objectList)
     passDelegate(&(new Helper(x,7)).opCall);
}


More information about the Digitalmars-d-learn mailing list