digitalmars.D.learn - opAssign work not with initialisation?
- spir (21/21) Dec 18 2010 Hello,
- Adam Burton (4/30) Dec 18 2010 That compiles for me. I am running 2.050 on linux. As a work-around try ...
- spir (11/29) Dec 18 2010 Does not with dmd 2.049 on (ubuntu) Linux. If it's solved in 2.050, then...
- Adam Burton (7/42) Dec 19 2010 Actually this is my bad, I forgot to add the unittest switch :-P. Still
Hello, struct S { int value; void opAssign(int value) { this.value =3D value; } } unittest { S s1; s1 =3D 3; // OK S s2 =3D 3; // _build_ error } =3D=3D> Element.d(105): Error: cannot implicitly convert expression (3) of type int= to S Do I miss something? And why not a compile-time error? Thank you, Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 18 2010
spir wrote:Hello, struct S { int value; void opAssign(int value) { this.value = value; } } unittest { S s1; s1 = 3; // OK S s2 = 3; // _build_ error } ==> Element.d(105): Error: cannot implicitly convert expression (3) of type int to S Do I miss something? And why not a compile-time error? Thank you, Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.comThat compiles for me. I am running 2.050 on linux. As a work-around try "S s2 = S(3)", failing that then try adding a constructor that accepts an int (opAssign is only used during assignment, not construction).
Dec 18 2010
On Sat, 18 Dec 2010 13:08:14 +0000 Adam Burton <adz21c gmail.com> wrote:struct S { int value; void opAssign(int value) { this.value =3D value; } } unittest { S s1; s1 =3D 3; // OK S s2 =3D 3; // _build_ error } =3D=3D> Element.d(105): Error: cannot implicitly convert expression (3) of type int to SThat compiles for me. I am running 2.050 on linux.Does not with dmd 2.049 on (ubuntu) Linux. If it's solved in 2.050, then le= t us let down.As a work-around try "S=20 s2 =3D S(3)", failing that then try adding a constructor that accepts an =int=20(opAssign is only used during assignment, not construction).;-) Sure, but this bypasses opAssign and assigns .value directly, I guess. = What do you think? Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Dec 18 2010
spir wrote:On Sat, 18 Dec 2010 13:08:14 +0000 Adam Burton <adz21c gmail.com> wrote:Actually this is my bad, I forgot to add the unittest switch :-P. Still happens in 2.050.struct S { int value; void opAssign(int value) { this.value = value; } } unittest { S s1; s1 = 3; // OK S s2 = 3; // _build_ error } ==> Element.d(105): Error: cannot implicitly convert expression (3) of type int to SThat compiles for me. I am running 2.050 on linux.Does not with dmd 2.049 on (ubuntu) Linux. If it's solved in 2.050, then let us let down.Yes it will. AFAIK that is how it is supposed to work, opAssign is used for assignment to existing variables and the constructor or initialisation is used when creating it. So even if the above compiled you wouldn't get your call to opAssign in "S s2 = 3".As a work-around try "S s2 = S(3)", failing that then try adding a constructor that accepts an int (opAssign is only used during assignment, not construction).;-) Sure, but this bypasses opAssign and assigns .value directly, I guess. What do you think?Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com
Dec 19 2010