The while-else-loop

The while-else-loop

Sometimes, „while“ and „if“ are interchangeable, for example while if you are „iterating“ over a collection that will only contain one or zero elements. In Java, both constructs have the same syntax, so I often find myself switching from one to the other.
Yesterday, I didn’t notice that my „if“ construct already had an „else“ branch, and so I created a while-else-loop. It took Eclipse like 10 seconds to realize that there’s something wrong, so when I saw my error after about 5 seconds, I instantly though „maybe that’s valid Java, and nobody ever noticed…“. Well it isn’t, but now I’m asking: why? And while I’m talking about the while-else-loop, what about for-else?
Often, you find yourself dealing with empty collections manually, like this:

Collection<Stuff> myStuff = getStuff();
if(myStuff.isEmpty())
{
    System.out.println("I've got no stuff.");
}
else
{
    for(Stuff s : myStuff)
    {
        System.out.println("I've got " + s.getDescription());
    }
}

Wouldn’t it be nice to have something like

Collection<Stuff> myStuff = getStuff();
for(Stuff s : myStuff)
    System.out.println("I've got " + s.getDescription());
else
    System.out.println("I've got no stuff.");

That would be pretty neat. But once you think about common loop idioms, there’s even more to invent.
And sometimes, you have to do something before the first and after the last line, but not if there are no lines. Or you have to do something between the lines, or just before the last one. Imagine your collections contains {socks,cups,dust,chocolate,nails} and you want to create an output like this:
I’ve got socks, cups, dust, chocolate and nails.
Normally, this involves heavy if-else action, and counting, like that:

Collection<Stuff> myStuff = getStuff();
if (myStuff.isEmpty()) {
	System.out.println("I've got no stuff.");
} else {
	int size = myStuff.size();
	int counter = 0;
	System.out.print("I've got ");
	for (Stuff s : myStuff) {
		System.out.print(s.getDescription());
		if (counter < size - 1)
			System.out.print(", ");
		else
			System.out.print(" and ");
		counter++;
	}
	System.out.print(".n");
}

Imagine writing it like that:

Collection<Stuff> myStuff = getStuff();
iterate(Stuff s : myStuff)
{
    before:
        System.out.print("I've got ");
    for:
        System.out.print(s.getDescription());
    between:
        System.out.print(", ");
    beforelast:
        System.out.print(" and ");
    after:
        System.out.print(".n");
    empty:
        System.out.print("I've got no stuff.n");
}

For While it may be overkill to put this into the Java language, I think it might be the right time for me to look into a language that has the degree of flexibility that allows me to introduce such constructs myself, without patching the compiler.
PS: Any ideas how to get syntax highlighting  into wordpress, given the fact that I want to use it for non-existing syntax constructs?

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.