Struct Union behavior
Voitech via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jan 6 03:39:44 PST 2016
Hello, i am new to D language and trying to learn it by coding.
I compile my programs on Xubuntu 14.04 with DMD64 D Compiler
v2.069.2.
So i have a struct/union which contains two fields representing
real and string values:
public union Element
{
private real _value;
private string _rawValue;
@property{
real value(){
return _value;
}
void value(real value){
_value=value;
}
}
@property {
ref string rawValue(){
return _rawValue;
}
void rawValue(ref string value){
_rawValue=value;
}
}
public Element opBinary(string op)(Element other){
Element newElement;
real value=mixin("this.value "~op~"other.value");
newElement.value=value;
return newElement;
}
public bool opEquals(Element other){
if(this.rawValue == null && other.rawValue == null){
return this.opEquals(other.rawValue);
}
return this.opEquals(other.value);
}
public bool opEquals(real value){
return this.value==value;
}
public bool opEquals(string rawValue){
return this.rawValue==rawValue;
}
unittest{
Element e1 = {4},e2 ={5};
writeln("e1 is at address: ",&e1);
writeln("e2 is at address: ",&e2);
writeln("e1.value is at address: ",&e1._value);
writeln("e2.value is at address: ",&e2._value);
assert(e1+e2==9);
}
unittest{
Element s1={_rawValue:"+"},s2,s3,s4;
writeln("s1 is at address: ",&s1);
writeln("s2 is at address: ",&s2);
writeln("s3 is at address: ",&s3);
writeln("s1.value is at address: ",&s1._value);
writeln("s2.value is at address: ",&s2._value);
writeln("s3.value is at address: ",&s3._value);
writeln("s4.value is at address: ",&s4._value);
writeln("s1.rawValue ",s1._rawValue);
writeln("s1.value: ",s1._value);
writeln("s2.value: ",s2._value);
writeln("s3.value: ",s3._value);
writeln("s4.value: ",s4._value );
assert(s1!=s2);
}
}
The unit test results are:
e1 is at address: 7FFCEFBA7510
e2 is at address: 7FFCEFBA7520
e1.value is at address: 7FFCEFBA7510
e2.value is at address: 7FFCEFBA7520
s1 is at address: 7FFCEFBA7500
s2 is at address: 7FFCEFBA7510
s3 is at address: 7FFCEFBA7520
s1.value is at address: 7FFCEFBA7500
s2.value is at address: 7FFCEFBA7510
s3.value is at address: 7FFCEFBA7520
s4.value is at address: 7FFCEFBA7530
s1.rawValue +
s1.value: 0.125
s2.value: 4
s3.value: 5
s4.value: 9
Can anyone tell me why s2,s3,s4 have initialized _value field?
Shouldn't it be collected by GC when first unit test (with e1,e2)
finishes ? If not how to handle this behavior, to use union
without preinitialized fields.
More information about the Digitalmars-d-learn
mailing list