Thread overview
std.concurrency and const
Jan 05, 2022
Christian Köstlin
Jan 06, 2022
frame
Jan 06, 2022
Christian Köstlin
January 05, 2022

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.

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

January 06, 2022

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.

January 06, 2022

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