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

Sean Kelly sean at invisibleduck.org
Wed Jan 13 09:07:00 PST 2010


On Jan 13, 2010, at 7:44 AM, Mark Kegel wrote:

> The reason I'm scared is fairly simple, I've seen this exact design
> (well maybe not exact, but the similarities are too great to ignore)
> used before. I worked for a couple of years on a large Telco system.
> Message handling was its bread and butter. We had lots of independent
> process with probably thousands of different messages flying through
> the system.

I had my start in telco as well, which I suppose is why I'm so soft on Erlang (we used C++ top to bottom).

> One thing that got beat into my head was that you really, really want
> to be able to easily trace the sequence of messages in a system. What
> pattern matching gives you is a more concise notation for telling
> outside users what is handled and what is not. That is the protocols
> that actors use to communicate are made explicit, not left implicit
> and thus recalcuated everytime you want to know how it works. In
> essence you can segment your code more cleanly along logical
> communication lines rather than function signature, since one function
> signature might be the entry point for fifteen different completely
> orthogonal messages.

I've been trying to match the Erlang receive syntax as closely as possible.  You're right that what is still missing is a clearly readable run time pattern matching syntax. I hope that someone will create something with templates and such to take care of this.

> It really all comes down to how easily the syntax of the language
> scales to large designs. Sure, taking anonymous functions and then
> testing for all messages that matching that signature is exactly as
> powerful as pattern matching, but I would contend that its a lot less
> readable.
> 
> So lets assume that you let receive() take some kind of pattern
> object, what might this look like? To write it out in the worst
> possible way, here's one idea:
> 
> receive( pattern("foo", _1) + (string a, string b) { ...},
>            pattern("bar", _1) + (string a, string b) { ...},
>            pattern(_1, _2) + (string a, string b) { ...},
>            ...
>          );
> 
> I've used _* placeholders similar to the way boost lambda does.
> 
> While I've intentionally made this as ugly (and self documenting) as
> possible, I really have no idea how'd you'd clean this syntax up,
> since I'm just not that familiar with D's introspection capabilites.
> Does any one more familiar with D have a better way to implement
> pattern matching?

I'd imagine patterns typically use constants, so you could at least do something like this:

    receive( test("p1 == \"foo\" || p2 == 5)( (string a, int b) {...} ) );

though this could still use a lot of improvement.


More information about the dmd-concurrency mailing list