if-expressions
Bent Rasmussen
incredibleshrinkingsphere at gmail.com
Mon May 28 00:42:29 PDT 2007
I've been away from D for quite a while, but am anxious to get back and use
the new features.
Reading the newsgroup to catch up a little, I remembered a feature of
another language (haXe) I've been using for some light-weight work. It has
the nice property that `if´ is an expression. So I was wondering if this
would be nice in D as well...
int lim(int x, int a, int b)
{
return if (x < a)
a
else if (x > b)
b
else
x;
}
But it could also be written
int lim(int x, int a, int b)
{
return x < a ?
a
: x > b ?
b
:
x;
}
The question is if very nested ?: expressions will look very readable, in
isolation and compared to if's.
I've found if-expressions to be changing my coding style and making my code
cleaner and a lot more readable. The same goes for switch-expressions. Many
functions have turned into pure expressions with just one point of return.
I'll try and recode some libraries using ?: expressions to begin with and
see how it shapes up visually.
If this all sounds stupid, be gentle, I'm just returning to the D game
again. :-)
Regards
Bent
More information about the Digitalmars-d
mailing list