www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - immutable(ubyte)[] to receive

reply Alexey <makovey090601 gmail.com> writes:
Maybe someone knows why it doesn't work?
```
import std.concurrency;
import std.stdio: write, writeln, writef, writefln;
import std.variant : Variant;
import std.process : thisThreadID;

alias Buffer = immutable(ubyte)[];

void pros2(string name)
     {
      receive(

             (immutable(Buffer[]) h){
                 writeln(h);
                 ownerTid.send(123);
             }
      );
  }
void main()
{

     Buffer[] t ;
     t ~= cast(Buffer)"sdfsdfsdf";
     t ~= cast(Buffer)"sdfsdfsdf";
     immutable(Buffer[]) ti = cast(immutable)t;


     auto tid2 = spawn(&pros2, "hello");
     send(tid2, ti);
     receive(
        (int h){writeln(h);}
     );
  }
```
Jan 20 2022
next sibling parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:
     Buffer[] t ;
     t ~= cast(Buffer)"sdfsdfsdf";
     t ~= cast(Buffer)"sdfsdfsdf";
     immutable(Buffer[]) ti = cast(immutable)t;
This is undefined behavior. Since ti isn't really immutable. You can still modify the original buffer (t). You should use const instead of immutable and, that goes for your functions too. They should receive const and not immutable. Summary: Mutable means: Anyone and anything can modify it. Immutable means: Nobody will modify it, not here and not anywhere else. Initialization is only allowed, but not pointing to anything that has mutable references. Const means: I promise not to modify it, but another mutable reference might. In your example t is a mutable reference, where as ti is an immutable reference. That's illegal and constitutes as UB (Undefined behavior) It would have been legal if you either used const or if you made an immutable duplication of t with ex. t.idup.
Jan 20 2022
parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote:
 On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:
     Buffer[] t ;
     t ~= cast(Buffer)"sdfsdfsdf";
     t ~= cast(Buffer)"sdfsdfsdf";
     immutable(Buffer[]) ti = cast(immutable)t;
This is undefined behavior. Since ti isn't really immutable. You can still modify the original buffer (t). You should use const instead of immutable and, that goes for your functions too. They should receive const and not immutable. Summary: Mutable means: Anyone and anything can modify it. Immutable means: Nobody will modify it, not here and not anywhere else. Initialization is only allowed, but not pointing to anything that has mutable references. Const means: I promise not to modify it, but another mutable reference might. In your example t is a mutable reference, where as ti is an immutable reference. That's illegal and constitutes as UB (Undefined behavior) It would have been legal if you either used const or if you made an immutable duplication of t with ex. t.idup.
Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...
Jan 20 2022
parent reply bauss <jj_1337 live.dk> writes:
On Thursday, 20 January 2022 at 10:19:37 UTC, bauss wrote:
 On Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote:
 On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:
     Buffer[] t ;
     t ~= cast(Buffer)"sdfsdfsdf";
     t ~= cast(Buffer)"sdfsdfsdf";
     immutable(Buffer[]) ti = cast(immutable)t;
This is undefined behavior. Since ti isn't really immutable. You can still modify the original buffer (t). You should use const instead of immutable and, that goes for your functions too. They should receive const and not immutable. Summary: Mutable means: Anyone and anything can modify it. Immutable means: Nobody will modify it, not here and not anywhere else. Initialization is only allowed, but not pointing to anything that has mutable references. Const means: I promise not to modify it, but another mutable reference might. In your example t is a mutable reference, where as ti is an immutable reference. That's illegal and constitutes as UB (Undefined behavior) It would have been legal if you either used const or if you made an immutable duplication of t with ex. t.idup.
Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...
I'm too fast again, well don't ignore entirely what I said, since you still modify an immutable reference by appending to t, I believe. I'm not 100% sure about this one, but I'm 99% sure.
Jan 20 2022
next sibling parent Alexey <makovey090601 gmail.com> writes:
On Thursday, 20 January 2022 at 10:21:23 UTC, bauss wrote:
 On Thursday, 20 January 2022 at 10:19:37 UTC, bauss wrote:
 On Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote:
 [...]
Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...
I'm too fast again, well don't ignore entirely what I said, since you still modify an immutable reference by appending to t, I believe. I'm not 100% sure about this one, but I'm 99% sure.
problem: we cant receive Buffer[], solution is to make some wrapper, but its interesting why we cant without
Jan 20 2022
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/20/22 5:21 AM, bauss wrote:
 On Thursday, 20 January 2022 at 10:19:37 UTC, bauss wrote:
 On Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote:
 On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:
     Buffer[] t ;
     t ~= cast(Buffer)"sdfsdfsdf";
     t ~= cast(Buffer)"sdfsdfsdf";
     immutable(Buffer[]) ti = cast(immutable)t;
This is undefined behavior. Since ti isn't really immutable. You can still modify the original buffer (t). You should use const instead of immutable and, that goes for your functions too. They should receive const and not immutable. Summary: Mutable means: Anyone and anything can modify it. Immutable means: Nobody will modify it, not here and not anywhere else. Initialization is only allowed, but not pointing to anything that has mutable references. Const means: I promise not to modify it, but another mutable reference might. In your example t is a mutable reference, where as ti is an immutable reference. That's illegal and constitutes as UB (Undefined behavior) It would have been legal if you either used const or if you made an immutable duplication of t with ex. t.idup.
Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...
I'm too fast again, well don't ignore entirely what I said, since you still modify an immutable reference by appending to t, I believe. I'm not 100% sure about this one, but I'm 99% sure.
Note that casting from mutable to immutable is actually defined as long as you don't modify the data after that (as seen [here](https://dlang.org/spec/const3.html#creating_immutable_data)). It's actually technically defined to cast to mutable from immutable as long as you don't modify the mutable data (this can be a requirement if you are calling a C function for example that takes a mutable reference that you know won't modify the data for that call). Appending to immutable array data is not considered a modification, because within the context of the language, an append is like tacking on unallocated data to the end of the array. -Steve
Jan 20 2022
prev sibling next sibling parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:
 Maybe someone knows why it doesn't work?
 ```
 import std.concurrency;
 import std.stdio: write, writeln, writef, writefln;
 import std.variant : Variant;
 import std.process : thisThreadID;

 alias Buffer = immutable(ubyte)[];

 void pros2(string name)
     {
      receive(

             (immutable(Buffer[]) h){
                 writeln(h);
                 ownerTid.send(123);
             }
      );
  }
 void main()
 {

     Buffer[] t ;
     t ~= cast(Buffer)"sdfsdfsdf";
     t ~= cast(Buffer)"sdfsdfsdf";
     immutable(Buffer[]) ti = cast(immutable)t;


     auto tid2 = spawn(&pros2, "hello");
     send(tid2, ti);
     receive(
        (int h){writeln(h);}
     );
  }
 ```
It's a Phobos bug. ``` import std.variant; void main() { alias A = immutable(ubyte)[]; auto a = Variant(immutable(A[]).init); assert(a.convertsTo!(immutable(A[]))); } ``` File a bug report, please?
Jan 20 2022
parent Alexey <makovey090601 gmail.com> writes:
On Thursday, 20 January 2022 at 11:24:18 UTC, FeepingCreature 
wrote:
 On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:
 [...]
It's a Phobos bug. ``` import std.variant; void main() { alias A = immutable(ubyte)[]; auto a = Variant(immutable(A[]).init); assert(a.convertsTo!(immutable(A[]))); } ``` File a bug report, please?
thanks, I will
Jan 20 2022
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
There is a workaround that makes more sense to me.

On 1/20/22 01:38, Alexey wrote:

              (immutable(Buffer[]) h)
That requires not only that the elements are immutable but also the array is immutable. Although that may exactly be what is required, I suspect what actually meant is: 1) I want an array of immutable(Buffer) 2) I would like to be protected from modifying it by accident The same can be achieved by the following two options as well: a) (const immutable(Buffer)[] h) b) (in immutable(Buffer)[] h) The latter is better especially when compiled with -preview=in. It may very well be a Phobos bug but what I prefer above also seems to be a workaround in this case. :) Ali
Jan 20 2022