Const hole

Steven Schveighoffer schveiguy at gmail.com
Sun May 15 02:41:34 UTC 2022


I accidentally found that I was implicitly casting in my code a const 
vibed Json type to a mutable Json type.

I thought "that can't be right, how does that even work? Especially if 
the Json is an object or an array!".

But it does. At least since 2014.

See https://issues.dlang.org/show_bug.cgi?id=12885

Enjoy a nice cast-free way to use sumtype to unconstify references in 
@safe code!

```d
import std.sumtype;

void main() @safe
{
     alias MyType = SumType!(int, char[]);
     auto mt = const(MyType)("hello");
     MyType m2 = mt;
     char[] result = m2.tryMatch!((char[] a) => a);
     result[0] = 'j'; // segfault
}
```

-Steve


More information about the Digitalmars-d mailing list