immutable(ubyte)[] to receive

bauss jj_1337 at
Thu Jan 20 10:18:03 UTC 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.


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.

More information about the Digitalmars-d mailing list