Weird rule for immutable

Victor Porton porton at narod.ru
Sat Apr 11 01:19:29 UTC 2020


https://dlang.org/spec/const3.html has a weird rule:

"The second way is to cast data to immutable. When doing so, it 
is up to the programmer to ensure that any mutable references to 
the same data are not used to modify the data after the cast."

This rule is weird because it disallows the following code:

int[] x = [];
{
   immutable y = cast(immutable)x;
}
x = [1, 2];

Here x is modified only after the immutable reference goes out of 
scope.

Why this is disallowed? Is it the language designers' intention? 
Why? Or is it bad wording in the specification?

Consider another example, I assume also invalid(?):

void f(ref immutable int v) { }

int x = 1;
f();
x = 2; // the immutable reference does not exist at this point
        // but the assignment is disallowed(?)


More information about the Digitalmars-d mailing list