Truly algebraic Variant and Nullable

jmh530 john.michael.hall at gmail.com
Tue Dec 22 20:03:18 UTC 2020


On Tuesday, 22 December 2020 at 17:20:03 UTC, ag0aep6g wrote:
> On Tuesday, 22 December 2020 at 16:53:11 UTC, jmh530 wrote:
>> For
>> v = cast(size_t) x;
>> I thought @safe prevented explicitly casting an immutable to a 
>> mutable, but the code below seems to suggest it is ok in this 
>> case...
>>
>> void main() @safe
>> {
>>     immutable x = 32;
>>     auto v = cast(size_t) x;
>> }
>
> It's ok because you're making a copy. Casting from immutable to 
> mutable is only dangerous when altering the mutable thing would 
> affect the immutable thing, as it happens with pointers and 
> such.

Ah, I get the error when I keep v as a pointer (or replace x with 
a cast). I had tried below and didn't have errors, but if you 
change the cast to cast(size_t*) then you get the error. Thanks 
for that.

void main() @safe
{
     immutable int* x = new int(32);
     auto v = cast(size_t) x;
     v++;
}


More information about the Digitalmars-d-announce mailing list