immutable promise broken in unions?

Tobi G. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 2 07:04:39 PST 2016


On Saturday, 2 January 2016 at 10:04:47 UTC, Shriramana Sharma 
wrote:
>
> I thought the promise of `immutable` was: never changes.

The compiler doesn't protect you by carrying a bomb. :)

But there is another usecase where it makes sense to allow 
writing to other union members despite the overlap with immutable 
data:

>enum Types {Integer, Text, CalculationConstant}
>
>struct oldStyleLimitedBadVariant // Don't use that! D offers 
>std.variant.Algebraic..
>{
>    Types type;
>    union
>    {
>        int integer;
>        string text;
>        immutable float calculationConstant;
>    }
>}

There i would expect that i can write to integer and text at any 
time.

togrue




More information about the Digitalmars-d-learn mailing list