digitalmars.D.learn - opAssign work not with initialisation?
- spir <denis.spir gmail.com> Dec 18 2010
- Adam Burton <adz21c gmail.com> Dec 18 2010
- Adam Burton <adz21c gmail.com> Dec 19 2010
- spir <denis.spir gmail.com> Dec 18 2010
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.com
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
spir wrote: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 = 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
That 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.
happens in 2.050.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?
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".Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com
Dec 19 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 S
That 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 =
(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









Adam Burton <adz21c gmail.com> 