www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A humble request.

reply pragma <EricAnderton at yahoo dot com> <pragma_member pathlink.com> writes:
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
Aug 03 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"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.
Aug 03 2004
parent reply pragma <EricAnderton at yahoo dot com> <pragma_member pathlink.com> writes:
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
Aug 03 2004
parent reply Juanjo =?ISO-8859-15?Q?=C1lvarez?= <juanjuxNO SPAMyahoo.es> writes:
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.
Aug 03 2004
parent pragma <EricAnderton at yahoo dot com> <pragma_member pathlink.com> writes:
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
Aug 03 2004