that is bug?
Ali
fakeemail at example.com
Sat Apr 7 19:44:35 UTC 2018
On Saturday, 7 April 2018 at 18:52:56 UTC, Ali Çehreli wrote:
> On 04/07/2018 10:53 AM, Ali wrote:
> > On Saturday, 7 April 2018 at 15:26:56 UTC, Ali Çehreli wrote:
> >> On 04/07/2018 02:07 AM, sdvcn wrote:
> >>> string stt = "none";
> >>> true?writeln("AA"):writeln("BB"); ///Out:AA
> >>> true?stt="AA":stt="BB"; <<<<-----///Out:BB
> >>> writeln(stt);
> >>
> >> It is a bug because the behavior does not match the spec:
> >>
> >> https://issues.dlang.org/show_bug.cgi?id=18743
> >>
> >> Ali
> >
> > Hi Ali C
> >
> > I think it also a bug because the ternary seem to be
> returning the
> > second part
>
> Maybe... but the following is not a good test for that because
> the return value of the assignment operator would always be stt
> regardless of which expression is evaluated.
>
> >
> > try
> >
> > string stt = "none";
> > string b = "";
> > true?writeln("AA"):writeln("BB"); ///Out:AA
> > b = (true ? stt="AA":stt="BB"); ///Out:BB
> > writeln(stt);
> > writeln(b); ///Out:BB
>
> I tried something else and noticed that it doesn't actually
> evaluate the third expression because b is never changed:
>
> import std.stdio;
>
> void main() {
> int a;
> int b;
> int * c = &(true ? a = 1 : b = 2);
> writefln("a:%s %s", a, &a);
> writefln("b:%s %s", b, &b);
> writefln("c: %s", c);
> }
>
> Output:
>
> a:2 7FFDBBF57DB0 <-- Got the value of the third expression
> (BAD)
> b:0 7FFDBBF57DB4 <-- Not changed (good)
> c: 7FFDBBF57DB0 <-- Address of a (good)
>
> So, the expression correctly decides to affect and returns 'a'
> but uses the wrong value to assign.
>
> Ali
this kinda explains what happens to me
try
string stt = "none";
string b = "";
true?writeln("AA"):writeln("BB");
b = (true ? stt="AA": stt )="BB";
writeln(stt);
writeln(b);
output
AA
BB
BB
now try
string stt = "none";
string b = "";
true?writeln("AA"):writeln("BB"); ///Out:AA
(b = (true ? stt="AA": stt ))="BB"; ///Out:BB
writeln(stt);
writeln(b);
output
AA
AA
BB
so it seems
that since
b = (true ? stt="AA": stt )="BB";
and
b = true ? stt="AA": stt ="BB";
are equivalent
that
that the ternary operator return stt (after assigning it "AA")
then assign "BB" to it
More information about the Digitalmars-d
mailing list