Weird rule for immutable
Adam D. Ruppe
destructionator at gmail.com
Sat Apr 11 02:05:50 UTC 2020
On Saturday, 11 April 2020 at 01:45:20 UTC, Victor Porton wrote:
> But is there any practical reason that the specification
> disallows the following?
Any code that actually uses immutable data is allowed to make
various assumptions about it, like keeping the reference for
later, aggressively caching, etc.
Your code doesn't break that so it works (that's actually allowed
by "undefined behavior"; the language doesn't define what it
does, but you, the programmer, can know things the spec doesn't),
but in general code can do other things with it that would break
here.
> int[] x = [];
> {
> immutable y = cast(immutable)x;
> }
> x = [1, 2];
technically btw this code is not strictly wrong because it is the
REFERENCE where immutability matters, and here you are replacing
it with new data, not actually modifying the old. Just like
`string a = "foo"; a = "bar";` is allowed despite string being
immutable contents. But I'm pretending it means what you think it
means.
More information about the Digitalmars-d
mailing list