The new ?? and ??? operators

Bill Baxter dnewsgroup at billbaxter.com
Mon Sep 24 16:48:42 PDT 2007


Janice Caron wrote:
> On 9/24/07, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>> It would be great to have a way to express
>>
>>    complicated_expression > 0 ? complicated_expression : default
>>
>> Without having to duplicate complicated_expression, while still
>> remaining an expression overall.
> 
> (x = complicated_expression) > 0 ? x : default
> 
> You'd have to declare x first, but I don't see that as a major
> problem, 

It *is* a problem in a template where you can't use the 
name-matches-return-value trick if there's any extraneous stuff defined 
in the template.  Like so

template ComputeAConstant(Foo)
{
    const ComputeAConstant =
       complicated_expression(Foo) > 0 ?
         complicated_expression(Foo) :
           default;
}

if you stick an x in there you can no longer use it as just 
ComputeAConstant(float).

Of course that should just be fixed anyway, but it's also not so nice to 
have to declare something first in other situations.  Like switch cases 
or nested if-elseifs.

> and it does seem more sensible to me than creating new
> operators. (If there's one thing worse than keyword bloat, it's
> operator bloat).

At least these operators (?> ?< ?==) wouldn't require any new opBlah 
overloads.

--bb



More information about the Digitalmars-d mailing list