The new ?? and ??? operators

Arlen Albert Keshabyan arlen.albert at gmail.com
Sun Sep 23 12:20:36 PDT 2007


Chris Nicholson-Sauls Wrote:

> Arlen Albert Keshabyan wrote:
> > Vladimir Panteleev Wrote:
> > 
> >> On Sun, 23 Sep 2007 18:34:09 +0300, Arlen Albert Keshabyan <arlen.albert at gmail.com> wrote:
> >>
> >> [regarding ???]
> >>> The lvalue gets the first rvalue that evaluates to true (evaluation goes from left to right). 
> >> So the result can only be boolean?
> >> In this case, I don't understand how
> >>   a = b ??? c ??? d;
> >> is different from
> >>   a |= b || c || d;
> >>
> >> If b, c and d are false then a is unchanged.
> >>
> >> -- 
> >> Best regards,
> >>  Vladimir                          mailto:thecybershadow at gmail.com
> > 
> > 
> > Consider this:
> > 
> > int v = 15;
> > int v2 = 30;
> > 
> > int a = v > 20 ??? v = 17 ??? v2 < 25 ??? 5;
> > 
> > in this case 'a' evaluates to 5 because none of the conditions evaluate to true except the last one. if you remove the last condition (??? 5) then 'a' stays 0 in this case.
> > 
> > If v2 = 23 then 'a' evaluates to 23;
> > If v = 27 then 'a' evaluates to 27;
> > 
> > etc.
> > 
> > It means that it takes a sequential rvalue, tests it against a condition and if the condition evaluates to true then it assigns that rvalue to lvalue. It does not mean you are restricted to boolean values only.
> 
> Or in other words this:
> 
> int a = v   > 20
>      ??? v  == 17
>      ??? v2  < 25
>      ??? 5
> ;
> 
> Is supposed to be shorthand for this?
> 
> int a;
> if (v  > 20 ||v == 17)
>      a = v;
> else if (v2 < 25)
>      a = v2;
> else
>      a = 5;
> 
> 
> ...its an interesting thought, but it just feels more like a scripting language feature 
> than that of a systems language.  The other '??' version that just skips nulls could be 
> useful at times, though.  Hmm.
> 
> -- Chris Nicholson-Sauls


Yes. Exactly.



More information about the Digitalmars-d mailing list