On Sunday, 15 December 2013 at 00:36:31 UTC, Timon Gehr wrote:
I cannot cast data from my own storage allocator to immutable
because the behaviour will be undefined.
http://dlang.org/const3.html
Is this a documentation bug? What should be the actual rules?
Casting to immutable is defined, it is modifying the data which
is not. That is to say, by casting to immutable the compiler
cannot guarantee no mutation will occur. I assume you're
confusion comes from:
char[] s = ...;
immutable(char)[] p = cast(immutable)s; // undefined
behavior
immutable(char)[] p = cast(immutable)s.dup; // ok, unique
reference
The docs should probably be cleaned up, but it isn't exactly
incorrect, since a mutable reference exists the compiler can't
really define what behavior will occur if the program is run
because it can't guarantee what behavior exists.