Can we just have struct inheritence already?

Timon Gehr timon.gehr at gmx.ch
Fri Jun 14 01:46:40 UTC 2019


On 14.06.19 02:15, Jonathan M Davis wrote:
> Well, I fail to see how allowing access to private data is not @safe, since
> it does not inherently mean that there are going to be problems with memory
> safety.

int[N] array;
static assert(0<N);

struct VerifiedIndex{
     private int index_=0;
     @disable this(int); // shouldn't be necessary, but it is
     @property int index()@safe{ return index_; }
     @property void index(int i)@trusted{ // no, this can't be @safe
         enforce(0<=x&&x<N, "index out of bounds");
         index_=i;
     }
     int read()@trusted{ return array.ptr[index]; }
     void write(int x)@trusted{ array.ptr[index]=x; }
}

This kind of thing is necessary. You need to give @trusted a chance to 
provide a @safe interface. This is very hard if all @safe code can 
randomly assign to your private members.


More information about the Digitalmars-d mailing list