More on Rust

Jean Crystof a at a.a
Fri Feb 11 00:49:01 PST 2011


Jim Wrote:

> Jacob Carlborg Wrote:
> 
> > On 2011-02-10 20:15, Walter Bright wrote:
> > > Nick Sabalausky wrote:
> > >> "bearophile" <bearophileHUGS at lycos.com> wrote in message
> > >> news:iivb5n$na3$1 at digitalmars.com...
> > >>> auto x;
> > >>> if (localtime().hours >= 8) {
> > >>> x = "awake!"
> > >>> } else {
> > >>> x = "asleep, go away."
> > >>> }
> > >>> log "I'm " + x;
> > >>>
> > >>
> > >> That would be really nice to have in D.
> > >
> > >
> > > auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";
> > 
> > For this simple if statement it works but as soon you have a few lines 
> > in the if statement it will become really ugly. But one could wrap the 
> > if statement in a function instead. In other languages where statements 
> > really are expressions this works:
> > 
> > auto x = if (localtime().hours >= 8)
> >              "awake!";
> >           else
> >              "asleep, go away.";
> > 
> > log "I'm " + x;
> 
> 
> 
> Other languages may have bloated syntaxes, with no particular benefit.
> 
> 
> auto x = localtime().hours >= 8 ?
> 	"awake!"
> 	:
> 	"asleep, go away.";
> 
> log( "I'm " ~ x );

You're right. ? : is a masterpiece among syntaxes. Maybe a bit too terse, but it does exactly what people expect it to do and it has existed since C was born. 


More information about the Digitalmars-d mailing list