Truly algebraic Variant and Nullable

jmh530 john.michael.hall at gmail.com
Tue Dec 22 16:53:11 UTC 2020


On Tuesday, 22 December 2020 at 14:27:02 UTC, ag0aep6g wrote:
> On 22.12.20 04:56, 9il wrote:
>> 6. Algebraic type subsets are supported by `get`, 
>> `trustedGet`, `_is`, and `this` primitives. You can operate 
>> with algebraic subset as with the type of the original 
>> typeset. [1]
>
> "trustedGet" - That name smells of a safety violation. And 
> indeed (compile with `-release`):
>
> ----
> import mir.algebraic;
> import std.stdio;
> void main() @safe
> {
>     immutable int* x = new int(42);
>     Variant!(size_t, int*) v;
>     v = cast(size_t) x;
>     auto p = v.trustedGet!(int*); /* uh-oh */
>     *p = 13; /* mutating immutable */
>     writeln(*x); /* prints "13" */
> }
> [snip]

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;
}


More information about the Digitalmars-d-announce mailing list