On Wed, Sep 29, 2010 at 00:42:55 +1000, Fredderic Unpenstein wrote:
> On 28 September 2010 21:19, Julian Andres Klode <[email protected]> wrote:
> > On Mo, 2010-09-27 at 02:55 +0200, Frederik Sdun wrote:
> >> This is just for convinience, for the not unusual case of:
> >> if (args.length > 0)
> >> foreach(...)
> >> else
> >> ...
>
> Personally I find myself, in almost every language, wrapping for(each)
> statements in "if (the list has items) ... else ...", which is exactly
> what this feature addresses. And, it's essentially saying exactly
> what such a structure looks like; do this loop, else do this other
> thing if the loop never happened (because there weren't any items).
IMHO confusing and not really needed, because the if above is not that much
more writing.
> > That would be confusing for people with Python background, as 'else' is
> > executed when the loop is exhausted; that is if there was no
> > return/break in the loop body.
>
> That doesn't strike me as particularly useful, or as a particularly
> good definition of "else" for that matter. I'd consider that more of
> a "then" situation, with "else" used because it's already a handy
> reserved keyword that they couldn't think of a better use for.
Except, well, it's not. It is really else for cases when the loop is looking
for something. As in:
for x in cache:
if want(x):
break
else:
cache += new_entry(),
> Lets not punish Vala for Python's lack of good taste, though "then" as
> in "Python's for...else" could be handy too. ;)
In fact I can see more use for the python case. As I said, writing the
if(no-elements) before the loop is easy and makes your intention clear. On
the other hand I've seen lot's of:
for(i = begin; i != end; ++i)
{
if(something(i))
{
found = true;
break;
}
}
if(!found)
something_else();
which is pretty ugly. If you move the variable out of the for initialization,
and provided the condition has no side-effects, be rewritten as:
for(i = begin; i != end; ++i)
if(something(i))
break;
if(i != end)
; // nothing
else // here is our little ELSE
something_else();
I believe that making else shortcut for this with the benefits that
- the else branch is in i's scope even if i is declared in the loop
initialization
- and the end codnition is not evaluated when it does not need to be
is both more obvious and more useful.
On a side note, I do like the else for try/catch. I'd say it's much more
a "than" situation there (because it's executed if the try succeeded), but
else is already being used for that purpose in other languages (python among
other).
--
Jan 'Bulb' Hudec <[email protected]>
_______________________________________________
vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list