Thread overview | |||||
---|---|---|---|---|---|
|
May 31, 2007 Helper objects to create delegates | ||||
---|---|---|---|---|
| ||||
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); } |
May 31, 2007 Re: Helper objects to create delegates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jason House | Jason House wrote: > 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);} static void delegate() create(foo x, int param) { return &(new Helper(x,param)).opCall; } > } > void example(){ > foreach(foo x; objectList) > passDelegate(Helper.create(x,7)); > } Like that? -- Daniel -- int getRandomNumber() { return 4; // chosen by fair dice roll. // guaranteed to be random. } http://xkcd.com/ v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/ |
May 31, 2007 Re: Helper objects to create delegates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep wrote:
>
> Jason House wrote:
>> 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);}
> static void delegate() create(foo x, int param)
> {
> return &(new Helper(x,param)).opCall;
> }
>> }
>> void example(){
>> foreach(foo x; objectList)
>> passDelegate(Helper.create(x,7));
>> }
>
> Like that?
>
> -- Daniel
>
... or very similar. I think I mean to do
passDelegate( &(new Helper(x,7)).opCall );
I found std.bind which provides the functionality I was looking for. The .ptr() threw me for a loop, but I have it down now.
passDelegate( bind(&x.func,7).ptr() );
|
Copyright © 1999-2021 by the D Language Foundation