Dealing with the interior pointers bug

Boris-Barboris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 22 11:38:59 PDT 2017


On Thursday, 22 June 2017 at 13:56:29 UTC, ag0aep6g wrote:
> For example, the type system guarantees that immutable data 
> never changes. But the compiler allows you to cast from 
> immutable to mutable and change the data. It's an invalid 
> operation, but the compiler is not expected to catch that for 
> you.
Casts are part of the type system. Yes, D type system allows 
invalid operations. It's not the compiler's fault, it's type 
system's fault.

unittest
{
     immutable int a = 4;
     int* b = cast(int*) &a;
     *b = 5;
     assert(*(&a) == 5);
     assert(a == 4);
}


More information about the Digitalmars-d-learn mailing list