[dmd-concurrency] Pattern matching on message receives, error handling

Andrei Alexandrescu andrei at erdani.com
Wed Jan 13 10:55:02 PST 2010


Sean Kelly wrote:
> It's worth adding that Erlang does provide pattern matching capabilities that have to be a run time in D, but I'm not sure this same capability is available in Scala, for example.  In our case, this will probably be handled manually by the user, with possibly some fancier way to do it later (metaprogramming can do quite a lot in D).  For example:
> 
>     receive(
>         (int x) { ... },
>         (string a, int b) { if (a == "blah") return false; ...; return true; }
>     );
> 
> Here, the second function checks string a to see if it matches a pattern, and returns true/false to indicate that there was a match.

For the OO crowd, I think this will be quite useful:

class Handler
{
     bool receive(int x) { ... }
     bool receive(int x, double y) { ... }
     bool receive(string s, int x) { ... }
     ...
}

auto h1 = new Handler;
auto h2 = new Handler;
receive(h1, h2); // dispatches automatically
                  // starting from h1

Sean, may I put this in confidence that you'll find the time to 
implement it? Walter, we may need some introspection chops for that too.


Andrei


More information about the dmd-concurrency mailing list