Can we just have struct inheritence already?

ag0aep6g anonymous at example.com
Thu Jun 13 21:24:01 UTC 2019


On 13.06.19 21:21, Tim wrote:
> import std.stdio;
> 
> // Returns newly allocated data with increased value if b is true.
> // Returns same data otherwise.
> const(int)* test(immutable int *data, bool b) @trusted
> {
>      int *data2;
>      if(!b)
>      {
>          writeln("b seems to be false");
>          // The cast can be trusted, because data2 will only be modified 
> if b is true.
>          data2 = cast(int*)data;
>      }
>      else
>      {
>          writeln("b seems to be true");
>          data2 = new int(*data);
>      }
> 
>      if(b)
>      {
>          writeln("b seems to be true");
>          *data2 += 1;
>      }
>      return data2;
> }
> 
> void main() @safe
> {
>      bool b = void;
>      immutable int *data = new immutable int(1);
>      writeln("data before: ", *data);
>      const int *data2 = test(data, b);
>      writeln("data after: ", *data);
>      writeln("data2 after: ", *data2);
> }

Nice one!

It's cute how bool, of all things, shares a safety-critical property 
with pointers: not all bytes make valid bools/pointers.


More information about the Digitalmars-d mailing list