Matching with std.concurrency receive

Jonathan M Davis jmdavisProg at gmx.com
Fri Dec 9 22:47:10 PST 2011


On Saturday, December 10, 2011 06:38:19 Adam wrote:
> Okie, having some trouble trying to do matches against classes /
> interfaces with templates using the std.concurrency's receive()
> function.
> 
> Here's a simple use case I have:
> 
> import std.stdio;
> import std.concurrency;
> import std.variant;
> 
> immutable interface BasicType {
> }
> 
> immutable class SubType : BasicType {
> }
> 
> immutable class A(T : BasicType) {
> }
> 
> void main() {
>     Tid tid = spawn(&thread);
>     immutable A!(SubType) instance = new immutable(A!(SubType))();
>     send(tid, instance);
>     while (true) {}
> }
> 
> void thread() {
>     receive(
>             (A!(BasicType) message)
>             {
>                 writeln("Received");
>             },
>             (Variant ant)
>             {
>                 writeln(ant.type());
>             }
>            );
> }
> 
> Running this outputs the Variant ant's type, so we see that it
> "falls through" to that pattern. However, if I switch the match from
> BasicType to SubType... it just hangs on me.
> 
> What horribly stupid thing am I doing here?

The type must match _exactly_. So, if the type you're passing is 
immutable(A!SubType), then you need to be receiving immutable(A!SubType).

That aside, I'm not sure that receive actually works with classes at the 
moment. I believe that there are some issues with Variant which make it not 
work.

http://d.puremagic.com/issues/show_bug.cgi?id=7069

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list