www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Const hole

reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
May 14 2022
parent bauss <jj_1337 live.dk> writes:
On Sunday, 15 May 2022 at 02:41:34 UTC, Steven Schveighoffer 
wrote:
 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
And the one pull request that should've fixed it has been closed (not merged) and with no further update or explanation. https://github.com/dlang/dmd/pull/9061
May 16 2022