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 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 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

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)
roundaround about a million times in PHP, C# and now in D. It'd be great to have
that kind of support in the language.
Cheers,
--AJG.
================================
2B || !2B, that is the question.