if-expressions

Daniel Keep daniel.keep.lists at gmail.com
Mon May 28 19:19:59 PDT 2007



Bent Rasmussen wrote:
> ...
> int lim(int x, int a, int b)
> {
>    return if (x < a)
>        a
>    else if (x > b)
>        b
>    else
>        x;
> }
> ...
> 
> Regards
> Bent

I've always liked this feature in languages that have it.  It was one of
those "eureka!" moments when I first saw it in Nemerle.  Vote++;

Incidentally, I don't think adding this would be very disruptive at all.
 if statements can't show up in expressions, so it wouldn't interfere
with any existing valid code.

The only thing I think that would be really weird is setting the
"result" of the statement.  In the case of multiple statements, you need
a way to set the result.  Maybe return or break could be used :)

In any case, it's something I'd definitely like to see in D.  Hell, I'd
also like to see this:

double[] as = [1.,2.,3.,4.,5.];
double[] bs = foreach(a;as) a*a;

List comprehensions would be cooler still, but I'm not holding my breath
on that one :P

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list