www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Foreach case with zero items.

reply AJG <AJG_member pathlink.com> writes:
Hi there,

I have a suggestion for an addition to the foreach syntax. Even though D might
not get it implemented, I'm very interested in your commments. So here it is:
How about a "no-items" clause?

I've thought about which keyword to use, and came up with "else" (which is
already a keyword), "ornone," and "otherwise," which are unlikely to be
identifiers anyway. Allow me to demonstrate:

// Currently:
string[] matches = r.match(expr);

if (matches.length == 0) {
// There are no items.
// Do some stuff.
} else {
foreach (string m; matches) {
// There are items.
// Do some other stuff.
}
}

// With suggestion:
foreach (string m; r.match(expr)) {
// There are items.
// Do some stuff.
} else {
// There are no items.
// Do some other stuff.
}

What do you guys think?
--AJG.


==========================
if (!sin) throw (rock[0]);
Jun 16 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
I never would've thought of it... but I do kinda like it.  The (if !length else
foreach) 
pattern does happen a good deal, and this does shrink it down quite a lot,
without any 
fuss.  Probably fairly straightforward to implement, as well.  The only catch I
see right 
off, is how this would operate in terms of opApply?  Maybe a special return
value set 
aside to mean, "I am empty so run the else clause."

-- Chris Sauls

AJG wrote:
 Hi there,
 
 I have a suggestion for an addition to the foreach syntax. Even though D might
 not get it implemented, I'm very interested in your commments. So here it is:
 How about a "no-items" clause?
 
 I've thought about which keyword to use, and came up with "else" (which is
 already a keyword), "ornone," and "otherwise," which are unlikely to be
 identifiers anyway. Allow me to demonstrate:
 
 // Currently:
 string[] matches = r.match(expr);
 
 if (matches.length == 0) {
 // There are no items.
 // Do some stuff.
 } else {
 foreach (string m; matches) {
 // There are items.
 // Do some other stuff.
 }
 }
 
 // With suggestion:
 foreach (string m; r.match(expr)) {
 // There are items.
 // Do some stuff.
 } else {
 // There are no items.
 // Do some other stuff.
 }
 
 What do you guys think?
 --AJG.
 
 
 ==========================
 if (!sin) throw (rock[0]);
Jun 16 2005
parent reply James Dunne <james.jdunne gmail.com> writes:
In article <d8to07$ikt$1 digitaldaemon.com>, Chris Sauls says...
I never would've thought of it... but I do kinda like it.  The (if !length else
foreach) 
pattern does happen a good deal, and this does shrink it down quite a lot,
without any 
fuss.  Probably fairly straightforward to implement, as well.  The only catch I
see right 
off, is how this would operate in terms of opApply?  Maybe a special return
value set 
aside to mean, "I am empty so run the else clause."

-- Chris Sauls

AJG wrote:
 Hi there,
 
 I have a suggestion for an addition to the foreach syntax. Even though D might
 not get it implemented, I'm very interested in your commments. So here it is:
 How about a "no-items" clause?
 
 I've thought about which keyword to use, and came up with "else" (which is
 already a keyword), "ornone," and "otherwise," which are unlikely to be
 identifiers anyway. Allow me to demonstrate:
 
 // Currently:
 string[] matches = r.match(expr);
 
 if (matches.length == 0) {
 // There are no items.
 // Do some stuff.
 } else {
 foreach (string m; matches) {
 // There are items.
 // Do some other stuff.
 }
 }
 
 // With suggestion:
 foreach (string m; r.match(expr)) {
 // There are items.
 // Do some stuff.
 } else {
 // There are no items.
 // Do some other stuff.
 }
 
 What do you guys think?
 --AJG.
 
 
 ==========================
 if (!sin) throw (rock[0]);
I really like this. I've found myself doing the if (length == 0) { } else foreach ... quite a bit. It really would clean things up a bit. Regards, James Dunne
Jun 19 2005
parent AJG <AJG_member pathlink.com> writes:
I really like this.  I've found myself doing the if (length == 0) { } else
foreach ... quite a bit.  It really would clean things up a bit.
Exactly. I'm glad somebody else finds it useful. I've done the (length == 0) that kind of support in the language. Cheers, --AJG. ================================ 2B || !2B, that is the question.
Jun 19 2005