Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 18, 2010 opAssign work not with initialisation? | ||||
---|---|---|---|---|
| ||||
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 |
December 18, 2010 Re: opAssign work not with initialisation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | 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
That 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).
|
December 18, 2010 Re: opAssign work not with initialisation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam Burton | 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. > 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 |
December 19, 2010 Re: opAssign work not with initialisation? | ||||
---|---|---|---|---|
| ||||
Posted in reply to spir | 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. > Actually this is my bad, I forgot to add the unittest switch :-P. Still 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? 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". > > Denis > -- -- -- -- -- -- -- > vit esse estrany ☣ > > spir.wikidot.com |
Copyright © 1999-2021 by the D Language Foundation