Read-only property without @property
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Sat Sep 27 18:38:09 PDT 2014
On 9/27/14 3:45 AM, Daniel Murphy wrote:
> "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
I think that this is equivalent to u.q++, since the ++ does not apply to
the pointed-at variable, but at the pointer itself.
Weird, my tests showed that this was allowed.
More testing, I realize it's because it's a class variable.
This code compiles and runs:
class Foo
{
union
{
int x;
int *p;
}
void foo() @safe {*p = 5;}
}
void main() @safe
{
auto f = new Foo;
f.foo();
}
Which is similar to this code that does not:
void main() @safe
{
union U {
int x;
int *p;
}
U u;
*u.p = 5; // error
}
> Without unions, you can't create invalid pointers in @safe code.
> With unions, you can, but you can't access them.
The above shows you can in some situations, but I think that is a bug.
-Steve
More information about the Digitalmars-d
mailing list