Type-safe channels (Re: One year of Go)

Graham Fawcett fawcett at uwindsor.ca
Mon Nov 15 11:54:47 PST 2010


On Fri, 12 Nov 2010 18:15:15 -0500, Sean Kelly wrote:
> 
> To be honest, I haven't spent much time with Go because my cursory
> exposure to the language hasn't shown it to solve the problems I
> care about better than the languages I currently use. I think Go is
> in the right ballpark with channels though, and the rest is
> passable. I'm trying to withhold my opinion about the lack of
> generics and such and evaluate the language based on what's there.

Since you brought up channels...

One thing I very much like about Go (and Haskell, etc.) that I don't
think has a counterpart in Phobos is type-safe message passing. I like
the flexibility of the current "mailbox" design. But I'd really like
an alternative that could enforce type-safety on messages. I don't
think per-thread mailboxes could accommodate this, but Channel objects
could; e.g.:

struct Channel(T) { 
    T receive() { ... }
    void send(T val) { ... }
}

struct request { ... }
struct response { ... }

void worker(Channel!request inChan, Channel!response outChan) {
     while (true) {
        Request req = inChan.receive();
        Response resp = process(req);
        outChan.send(resp);
     }
}

Channel!Variant could be used to emulate thread mailboxes (though
without the per-thread limitation: multiple consumers could read from
the same channel.)

I think that type-safe channels would be a nice complement to the
current ad-hoc message-passing design. No reason I could see that they
couldn't coexist peacefully; having both would address a larger family
of possible concurrent designs, with different design trade-offs.

(I am guessing that tricks might have to be played to pass Channel
objects around as pure values across threads, but I'm also guessing
this is surmountable...)

Best,
Graham


More information about the Digitalmars-d mailing list