digitalmars.D - immutable(ubyte)[] to receive
- Alexey (29/29) Jan 20 2022 Maybe someone knows why it doesn't work?
- bauss (19/23) Jan 20 2022 This is undefined behavior.
- bauss (3/28) Jan 20 2022 Oh wait, I just noticed Buffer is an alias to an immutable array.
- bauss (5/44) Jan 20 2022 I'm too fast again, well don't ignore entirely what I said, since
- Alexey (3/15) Jan 20 2022 problem: we cant receive Buffer[], solution is to make some
- Steven Schveighoffer (12/58) Jan 20 2022 Note that casting from mutable to immutable is actually defined as long
- FeepingCreature (11/40) Jan 20 2022 It's a Phobos bug.
- Alexey (3/15) Jan 20 2022 thanks, I will
- =?UTF-8?Q?Ali_=c3=87ehreli?= (14/15) Jan 20 2022 That requires not only that the elements are immutable but also the
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
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
On Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote:On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...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
On Thursday, 20 January 2022 at 10:19:37 UTC, bauss wrote:On Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote: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.On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...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
On Thursday, 20 January 2022 at 10:21:23 UTC, bauss wrote:On Thursday, 20 January 2022 at 10:19:37 UTC, bauss wrote:problem: we cant receive Buffer[], solution is to make some wrapper, but its interesting why we cant withoutOn Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote: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.[...]Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...
Jan 20 2022
On 1/20/22 5:21 AM, bauss wrote:On Thursday, 20 January 2022 at 10:19:37 UTC, bauss wrote: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. -SteveOn Thursday, 20 January 2022 at 10:18:03 UTC, bauss wrote: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.On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:Oh wait, I just noticed Buffer is an alias to an immutable array. Ignore what I just said...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
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
On Thursday, 20 January 2022 at 11:24:18 UTC, FeepingCreature wrote:On Thursday, 20 January 2022 at 09:38:55 UTC, Alexey wrote:thanks, I will[...]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
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