Truly algebraic Variant and Nullable

ag0aep6g anonymous at example.com
Tue Dec 22 14:27:02 UTC 2020


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" */
}
----

The normal `get` also violates safety by giving out references into the 
union (compile with `-preview=dip1000`):

----
import mir.algebraic;
import std.stdio;
T* ref_to_ptr(T)(ref T r) @safe { return &r; }
void main() @safe
{
     immutable int* x = new int(42);
     Variant!(size_t, int*) v;
     int** p = ref_to_ptr(v.get!(int*)); /* uh-oh */
     v = cast(size_t) x;
     **p = 13; /* mutating immutable */
     writeln(*x); /* prints "13" */
}
----

But that might be an issue with DIP1000. `ref_to_ptr` is a hint that 
something isn't right in that area.


More information about the Digitalmars-d-announce mailing list