www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.concurrency and const

reply =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
Hi all,

I really like std.concurrency but I now stumbled upon the following.
When receiving messages as const, they also need to be sent as const 
(otherwise they are not matched). Comparing this to normal function 
calls I would expect a different behavior.


```d
import std.concurrency;
import std.stdio;

struct Message
{
}

void fun(const(Message) m) {
     writeln("fun(const(Message))");
}
/*
void fun(Message m) {
     writeln("fun(Message)");
}
*/
void receiver()
{
     receive(
         (Tid sender, const(Message) m)
         {
             writeln("received const(Message)");
         },
         (const(Variant) v)
         {
             writeln("Received const(Variant)", v);
         },
         (Variant v)
         {
             writeln("Received variant", v);
         },
     );
     writeln("done");
}
int main(string[] args)
{
     auto blubTid = spawnLinked(&receiver);
     blubTid.send(Message());

     receive(
         (LinkTerminated t)
         {
         }
     );

     fun(Message());
     fun(const(Message)());
     return 0;
}

```

output is something like:
```
Received variantMessage()
done
fun(const(Message))
fun(const(Message))
```

whereas I would expect
```
received const(Message)
done
fun(const(Message))
fun(const(Message))
```

Looking forward for the explanation of that.


Kind regards and happy 2022!
Christian
Jan 05 2022
parent reply frame <frame86 live.com> writes:
On Wednesday, 5 January 2022 at 22:22:19 UTC, Christian Köstlin 
wrote:
 Hi all,

 I really like std.concurrency but I now stumbled upon the 
 following.
 When receiving messages as const, they also need to be sent as 
 const (otherwise they are not matched). Comparing this to 
 normal function calls I would expect a different behavior.
They do. You have an error: `Message` doesn't match with `(Tid sender, const(Message) m)`. You don't get the argument `sender` here.
Jan 05 2022
parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 2022-01-06 02:55, frame wrote:
 On Wednesday, 5 January 2022 at 22:22:19 UTC, Christian Köstlin wrote:
 Hi all,

 I really like std.concurrency but I now stumbled upon the following.
 When receiving messages as const, they also need to be sent as const 
 (otherwise they are not matched). Comparing this to normal function 
 calls I would expect a different behavior.
They do. You have an error: `Message` doesn't match with `(Tid sender, const(Message) m)`. You don't get the argument `sender` here.
Duh ... you are right, my mistake!!! Thanks! Christian
Jan 06 2022