www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Perl's if and unless

reply Brian Hammond <d at brianhammond dot com> <Brian_member pathlink.com> writes:
I've never really liked Perl's general appearance except for its alternate if
and unless constructs.

These have the structure:

<statement> if <condition>;

<statement> unless <condition>;

e.g.

print "INFO: this is a message\n" 
if $verbose && $debug_level >= DBG_INFO;

process_input() unless length $input == 0;

The alternate if construct is equivalent to the D:

if (<condition>) 
{
<statement>;
}

The alternate unless construct is equivalent to the D:

if (! <condition>) 
{
<statement>;
}

I really like the unless because it reads so well.

"do this unless this is true"

I would think this bit of syntactic sugar wouldn't be too hard to add to D since
it's just a simple transformation.

I apologize if this has been requested and debated already.

Brian
May 21 2004
next sibling parent reply Juan C <Juan_member pathlink.com> writes:
<snip>
<statement> if <condition>;
<statement> unless <condition>;
</snip> Always makes me think of a bomb squad technician in a comedy: "Now cut the blue wire... <pause> but before you do that..."
May 21 2004
parent Stephen Waits <steve waits.net> writes:
Juan C wrote:
 <snip>
 
<statement> if <condition>;
<statement> unless <condition>;
</snip> Always makes me think of a bomb squad technician in a comedy: "Now cut the blue wire... <pause> but before you do that..."
I have to say that this is one of the ugliest constructs I've ever faced. :) --Steve [glad to say that my perl hacking days are mostly behind me..]
May 21 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"Brian Hammond" <d at brianhammond dot comBrian_member pathlink.com> wrote
in message news:c8lmu2$vdm$1 digitaldaemon.com...
 I really like the unless because it reads so well.

 "do this unless this is true"
That just seems backwards to me <g>. I like things to execute forwards, not backwards.
May 21 2004
parent reply Brian Hammond <d at brianhammond dot com> <Brian_member pathlink.com> writes:
 I really like the unless because it reads so well.

 "do this unless this is true"
That just seems backwards to me <g>. I like things to execute forwards, not backwards.
Well she's your baby so that's that :-) Brian
May 21 2004
parent "Phill" <phill pacific.net.au> writes:
"Brian Hammond" <d at brianhammond dot comBrian_member pathlink.com> wrote
in message news:c8mh1p$264r$1 digitaldaemon.com...
 I really like the unless because it reads so well.

 "do this unless this is true"
That just seems backwards to me <g>. I like things to execute forwards,
not
backwards.
Well she's your baby so that's that :-)
Thank god for that. It seems a bit strange to me as well. I dont like using a do / while either. Phill
May 22 2004