Send/Receive bug in std.concurrency with immutable(ubyte[]) ?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Aug 4 15:19:49 UTC 2019


On Sunday, August 4, 2019 8:29:05 AM MDT Paul Backus via Digitalmars-d 
wrote:
> On Sunday, 4 August 2019 at 13:57:14 UTC, Vijay Nayar wrote:
> > import std.concurrency;
> > import std.stdio;
> >
> > void testThread() {
> >
> >   receive(
> >
> >       (immutable(ubyte[]) data) => writeln("Got it!"),
> >       (Variant v) => writeln("Mismatch: ", v.type()));
> >
> > }
> >
> > void main() {
> >
> >   immutable(ubyte[]) data = [1, 2, 3];
> >   auto tid = spawn(&testThread);
> >   send(tid, data);
> >
> > }
> >
> > The output of this program is:
> >   Mismatch: immutable(ubyte)[]
> >
> > Why does calling send with data of type immutable(ubyte[]) get
> > converted to immutable(byte)[] when it is called with
> > std.concurrency's send() and receive().  Is this a bug?
>
> immutable(T[]) "decays" to immutable(T)[] when passed to a
> function (most of the time [1]):
>
> import std.stdio;
>
> void checkType(T)(T arg) { writeln(typeof(arg).stringof); }
>
> void main()
> {
>      immutable(ubyte[]) a;
>      checkType(a); // immutable(ubyte)[]
> }
>
> [1] https://issues.dlang.org/show_bug.cgi?id=18268

When you slice a const or immutable dynamic array, the type of the resulting
dynamic array is tail-const or tail-immutable. When templates are implicitly
instantiated with IFTI, and a dynamic array is passed in, the type is always
inferred using the type of the slice of the dynamic array. That's completely
consistent. You only get a fully const or immutable dynamic array with a
template when the template is explicitly instantiated with that type.

However, based on the examples in that bug report, what happens with lambdas
and function/delegate literals is not consistent - or at least, the reasons
why the type inference uses the type of the slice in some cases and the type
of the array being sliced in others isn't immediately clear.

- Jonathan M Davis





More information about the Digitalmars-d mailing list