Read-only property without @property

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 27 00:45:25 PDT 2014


"H. S. Teoh via Digitalmars-d"  wrote in message 
news:mailman.1799.1411796077.5783.digitalmars-d at puremagic.com...

> Argh, looks like another incompletely-implemented part of the compiler:
>
> void fun() @safe {
> union U {
> int  p;
> int* q;
> }
> U u;
> u.p++; // compiles
> u.q = null; // compiles
> *u.q++; // x.d(9): Error: field U.q cannot be accessed in @safe code 
> because > it overlaps with a pointer
> }

This looks perfectly fine to me.  Consider if you replaced all uses of the 
union with multiple variables and explicit casts:

int p;
int* q;

p++; // fine, of course
q++; // also fine

p = 0;
q = null;
// both of these are fine, because they don't read anything from the union.

// worst case, p was the last value assigned to
*(*cast(int**)&p)++; // The cast can't be @safe

Without unions, you can't create invalid pointers in @safe code.
With unions, you can, but you can't access them. 



More information about the Digitalmars-d mailing list