Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 03, 2004 A humble request. | ||||
---|---|---|---|---|
| ||||
Walter, I would like to submit an idea for two features that I feel are strong enough for version 2.0 at the very least. I would like to see added to D is an event or multicast delegate mechanism, similar to what C# has. I'd like to advocate expanding arrays of functions and delegates to allowing a call on the array to apply to all elements of the underlying array: delegate(char[] reason)[] onShutdown; onShutdown ~= &dllManager.unloadLibraries; onShutdown ~= &threadManager.stopThreads; onShutdown("The server is shutting down."); // calls everything in the array This could have some interesting implications on arrays of objects and opCall as well. My quest for this kind of functionality has lead me to write a template, for which i'd ideally like to do the following: template multicast(DT : RT delegate(T1)){ RT muticast(DT[] delegateArray,T1 one){ foreach(DT thisDelegate; delegateArray){ thisDelegate(one); } } } This leads me to my second request: is there any way that delegate and function parameter and return types may be treated in the same regard as first-class template parameters? Right now, it just isn't possible to specialize a template based on a generic delegate type. One can use a specific delegate type instead, but even then you still can't pick apart the delegate to get the return or parameter types. The following is the cannonical example of how dmd doesn't like this: // test.d template Foo(DT: RT delegate(T1,T2,T3)){ } test.d(2): identifier 'RT' is not defined // test2.d template Foo(RT delegate(T1,T2,T3)){ } test2.d(2): no identifier for template value parameter test2.d(2): Declaration expected, not ')' - Pragma |
August 03, 2004 Re: A humble request. | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | "pragma" <EricAnderton at yahoo dot compragma_member@pathlink.com> wrote in message news:ceodn0$1bhp$1@digitaldaemon.com... > Walter, > I would like to submit an idea for two features that I feel are strong enough > for version 2.0 at the very least. > > I would like to see added to D is an event or multicast delegate mechanism, > similar to what C# has. I'd like to advocate expanding arrays of functions and > delegates to allowing a call on the array to apply to all elements of the underlying array: > > delegate(char[] reason)[] onShutdown; > > onShutdown ~= &dllManager.unloadLibraries; > onShutdown ~= &threadManager.stopThreads; > onShutdown("The server is shutting down."); // calls everything in the array > > This could have some interesting implications on arrays of objects and opCall as > well. Can't foreach do this job? > > > My quest for this kind of functionality has lead me to write a template, for > which i'd ideally like to do the following: > > template multicast(DT : RT delegate(T1)){ > RT muticast(DT[] delegateArray,T1 one){ > foreach(DT thisDelegate; delegateArray){ > thisDelegate(one); > } > } > } > > This leads me to my second request: is there any way that delegate and function > parameter and return types may be treated in the same regard as first-class > template parameters? > > Right now, it just isn't possible to specialize a template based on a generic > delegate type. One can use a specific delegate type instead, but even then you > still can't pick apart the delegate to get the return or parameter types. > > The following is the cannonical example of how dmd doesn't like this: > > // test.d > template Foo(DT: RT delegate(T1,T2,T3)){ > } > test.d(2): identifier 'RT' is not defined > // test2.d > template Foo(RT delegate(T1,T2,T3)){ > } > > test2.d(2): no identifier for template value parameter > test2.d(2): Declaration expected, not ')' Ok, I see. |
August 03, 2004 Re: A humble request. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <ceojbj$1eaa$1@digitaldaemon.com>, Walter says... > >"pragma" <EricAnderton at yahoo dot compragma_member@pathlink.com> wrote in >> >> delegate(char[] reason)[] onShutdown; >> >> onShutdown ~= &dllManager.unloadLibraries; >> onShutdown ~= &threadManager.stopThreads; >> onShutdown("The server is shutting down."); // calls everything in the >array >> > >Can't foreach do this job? Yes it can, and it probably should. :) My desire here was to have a mechanism that would make an 'event' look and behave as a single delegate. This is both useful and dangerous, depending on how one looks at it. Personally, I feel that it would make for a more powerful and expressive grammar without extra syntactic clutter (i.e. extra keywords, simple loops or templates). >> >> The following is the cannonical example of how dmd doesn't like this: >> >> // test.d >> template Foo(DT: RT delegate(T1,T2,T3)){ >> } >> test.d(2): identifier 'RT' is not defined >> // test2.d >> template Foo(RT delegate(T1,T2,T3)){ >> } >> >> test2.d(2): no identifier for template value parameter >> test2.d(2): Declaration expected, not ')' > >Ok, I see. > Thank you for reading my post Walter. :) - Pragma |
August 03, 2004 Re: A humble request. | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | pragma wrote:
> My desire here was to have a mechanism that would make an 'event' look and
> behave as a single delegate. This is both useful and dangerous, depending
> on
> how one looks at it.
For example this is very usefull on GUI programming.
|
August 03, 2004 Re: A humble request. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Juanjo Álvarez | In article <ceoskl$1jaq$1@digitaldaemon.com>, Juanjo =?ISO-8859-15?Q?=C1lvarez?= says... > >pragma wrote: > >> My desire here was to have a mechanism that would make an 'event' look and >> behave as a single delegate. This is both useful and dangerous, depending >> on >> how one looks at it. > >For example this is very usefull on GUI programming. > Actually, I had that in mind when I wrote my original post. I'm pretty sure GUI programming was the motivating factor behind the event keyword in C# as well. If we're lucky enough for D to get templates expanded to specialize on delegate types, then it'd be easy to write a good class to cover this. :) - Pragma |
Copyright © 1999-2021 by the D Language Foundation