February 10, 2011
On 2011-02-10 20:15, Walter Bright wrote:
> Nick Sabalausky wrote:
>> "bearophile" <bearophileHUGS@lycos.com> wrote in message
>> news:iivb5n$na3$1@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;

-- 
/Jacob Carlborg
February 11, 2011
Jacob Carlborg Wrote:

> On 2011-02-10 20:15, Walter Bright wrote:
> > Nick Sabalausky wrote:
> >> "bearophile" <bearophileHUGS@lycos.com> wrote in message news:iivb5n$na3$1@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 );


If the expressions are complex I put them in functions.
1) It hides and isolates details, which allows you to focus on the more abstract aspects.
2) It gives the expression a name and facilitates reuse.
February 11, 2011
Jim Wrote:

> Jacob Carlborg Wrote:
> 
> > On 2011-02-10 20:15, Walter Bright wrote:
> > > Nick Sabalausky wrote:
> > >> "bearophile" <bearophileHUGS@lycos.com> wrote in message news:iivb5n$na3$1@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.
February 11, 2011
On 2011-02-11 08:39, Jim wrote:
> Jacob Carlborg Wrote:
>
>> On 2011-02-10 20:15, Walter Bright wrote:
>>> Nick Sabalausky wrote:
>>>> "bearophile"<bearophileHUGS@lycos.com>  wrote in message
>>>> news:iivb5n$na3$1@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 );
>
>
> If the expressions are complex I put them in functions.
> 1) It hides and isolates details, which allows you to focus on the more abstract aspects.
> 2) It gives the expression a name and facilitates reuse.

And that was the first thing I said one could do.

-- 
/Jacob Carlborg
February 11, 2011
On 02/11/2011 08:39 AM, Jim wrote:
> Jacob Carlborg Wrote:
>
>> On 2011-02-10 20:15, Walter Bright wrote:
>>> Nick Sabalausky wrote:
>>>> "bearophile"<bearophileHUGS@lycos.com>  wrote in message
>>>> news:iivb5n$na3$1@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 );
>
>
> If the expressions are complex I put them in functions.
> 1) It hides and isolates details, which allows you to focus on the more abstract aspects.
> 2) It gives the expression a name and facilitates reuse.

Agreed.
But in practice I often end up beeing dubious about seemingly nice features like, precisely, the ternary operator. In languages that do not have such a nicety people end up writng eg:
    if (localtime().hours>= 8) x = awake;
    else x = "asleep, go away.";
Apart from the very mild annoyance, I guess, of writing twice "x =", this is all gain: code is both more compact and legible.
Precisely, because the ternary operator is a bit weird syntactically (and not that commonly needed and used in real code), people feel, just like you, the need to clarify it in code, finally using more vertical space. Note that the case here is the simplest possible, expressions being plain literal constants. I personly only consume 3 lines:
    auto x = (localtime().hours>= 8)
        ? "awake!"
        : "asleep, go away.";
What do you think?

Denis
-- 
_________________
vita es estrany
spir.wikidot.com

February 11, 2011
spir Wrote:

> On 02/11/2011 08:39 AM, Jim wrote:
> > Jacob Carlborg Wrote:
> >
> >> On 2011-02-10 20:15, Walter Bright wrote:
> >>> Nick Sabalausky wrote:
> >>>> "bearophile"<bearophileHUGS@lycos.com>  wrote in message news:iivb5n$na3$1@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 );
> >
> >
> > If the expressions are complex I put them in functions.
> > 1) It hides and isolates details, which allows you to focus on the more abstract aspects.
> > 2) It gives the expression a name and facilitates reuse.
> 
> Agreed.
> But in practice I often end up beeing dubious about seemingly nice features
> like, precisely, the ternary operator. In languages that do not have such a
> nicety people end up writng eg:
>      if (localtime().hours>= 8) x = awake;
>      else x = "asleep, go away.";
> Apart from the very mild annoyance, I guess, of writing twice "x =", this is
> all gain: code is both more compact and legible.
> Precisely, because the ternary operator is a bit weird syntactically (and not
> that commonly needed and used in real code), people feel, just like you, the
> need to clarify it in code, finally using more vertical space. Note that the
> case here is the simplest possible, expressions being plain literal constants.
> I personly only consume 3 lines:
>      auto x = (localtime().hours>= 8)
>          ? "awake!"
>          : "asleep, go away.";
> What do you think?


I always make a conscious effort to write succinct expressions. My rule is to only use the ternary operator when it actually makes the code cleaner.

Why?

Branches should be conspicuous because they increase the number of code paths. Exceptions are the other big source of code paths. This is, btw, why I think that scope statements in D are just superb.
February 11, 2011
On 02/10/11 13:49, Andrej Mitrovic wrote:
> On 2/10/11, Walter Bright <newshound2@digitalmars.com> wrote:
>> auto x = (localtime().hours >= 8) ? "awake!" : "asleep, go away.";
> 
> Aye, a one liner!
> 
> I hate seeing things like this:
> if (funcall())
> {
>     var = "foo";
> }
> else
> {
>     var = "bar";
> }
> 
> So much clutter instead of using the simple:
> var = funcall() ? "foo" : "bar";
> 
> I also see this sometimes:
> 
> auto var = funcall();
> if (var == "foo" || var == "bar" || var == "foobar" || var == "barfoo")
> {
>     // some complex code
> }
> else if (var == "blue" || var == "green")
> {
>     // some complex code
> }
> else if ()// more and more code..
> { }
> 
> But not many people seem to know that D supports strings in case statements:
> switch(funcall())
> {
>     case "foo":
>     case "bar":
>     case "foobar":
>     case "barfoo":
>     {
>         // complex code
>     }
>     break;
>     case "blue":
>     case "green":
>     {
>        // complex code
>     }
>     break;
>     default:
> }
> 

Even better:

switch( funcall() ) {
    case "foo", "bar", "foobar", "barfoo": {
        // complex code
        break;
    }

    case "blue", "green": {
        // complex code
        break;
    }

    default:
        // do nothing -- i like to comment defaults
}

Also often forgotten, that 'case' clauses take an argument list, not just an expression.  And yeah, in this case at least... it still fits in 80 columns.  (I prefer 90 myself, but it's moot.)

-- Chris N-S
February 11, 2011
On 2/11/11, Christopher Nicholson-Sauls <ibisbasenji@gmail.com> wrote:
>
> Even better:
>
> switch( funcall() ) {
>     case "foo", "bar", "foobar", "barfoo": {
>         // complex code
>         break;
>     }
>
>     case "blue", "green": {
>         // complex code
>         break;
>     }
>
>     default:
>         // do nothing -- i like to comment defaults
> }
>
> Also often forgotten, that 'case' clauses take an argument list, not just an expression.  And yeah, in this case at least... it still fits in 80 columns.  (I prefer 90 myself, but it's moot.)
>
> -- Chris N-S
>

Damn I didn't know that! Thanks.
February 23, 2011
On 10/02/2011 00:23, bearophile wrote:
> But in Rust there are typestates, so while a variable can't change type, its type sometimes changes state along the flow of the code, such state of the type may be different in different parts of the code


Hum, this typestate concept actually looks quite interesting. I need to look in more detail, but it seems similar to some ideas I roughly considered several times before, about having a language being able to track which assertions are true for a given variable/value at any (or most) given points in code.
I'm fond of ideas that could make a static type system even more powerful (more checking capabilities), without making it significantly more obtuse or verbose. I wonder if many improvements could be made on this area, enough even for it to be considered a significant step forward in language design/evolution.
Oh how I wish that a day would have 48 hours...

-- 
Bruno Medeiros - Software Engineer
1 2
Next ›   Last »