[dmd-concurrency] std.concurrency

Robert Jacques sandford at jhu.edu
Wed Feb 24 08:30:46 PST 2010


On Wed, 24 Feb 2010 10:38:16 -0500, Mark Kegel <mark.kegel at gmail.com>  
wrote:
> All,
>
> I haven't seen this discussed yet, but could I make a small request?
> Would it be possible to add a mechanism to the receive() function so
> that if any one of the message handlers throws a certain exception,
> call it ContinueMessageDispatch, that when this exception is caught in
> receive(), receive() then tries the rest of the handlers with the
> message that's just come in. Thus a handler that didn't like a message
> (but for which the signature matched) could pass on it, but give
> another handler the opportuntity to process the message.
>
> This kind of mechanism would make it possible to write:
>
> receive(
>  (int a) {
>    if (a != 5)
>      throw new ContinueMessageDispatch();
>    else
>      doSomethingCool();
>  },
>  (int b) {
>    doSomethingelseCool();
>   }
> );
>
>
> Before anyone says "but these have the same signature, they should be
> combined!", let me state that I want this particular addition to make
> it easier to add in pattern matching as a library. Thus the above code
> could be written like this:
>
> receive( pattern!( _1 == 5) + (int a) {doSomethingCool();},
>             pattern!( _1 != 5) + (int b) {doSomethingelseCool();})
>
> I don't know about anyone else, but I really like this second bit of
> code much more than the first. But without the ability to continue
> dispatching I'm not sure it would be possible to implement this kind
> of pattern maching behavior.
>
> Mark Kegel

Mark, exceptions are not valid form of flow control. This is a fairly well  
known anti-pattern. Also, the operator overloading of '+' is generally  
considered poor design. That said, adding this functionality in a more  
formalized way would be useful. i.e. giving receive the explicit ability  
to handle patterns via struct wrappers by putting something like:

static if( is( typeof(handeler) == struct ) ) {
     if( handeler.match(msg) == false )
         continue;
}
handeler( msg );
break;

inside the receive message handling body.






More information about the dmd-concurrency mailing list