On Fri, Apr 26, 2002 at 05:24:13PM -0400, Aaron Sherman wrote:
> On Fri, 2002-04-26 at 14:11, Allison Randal wrote:
> > The "else" of a loop construct isn't really the same as the "else" of an
> > C<if>. You can't use an C<elsif> for one thing. 
> 
> Why not? What would be wrong with:
> 
>       for @x { 
>               ...
>       } elsif (stuff) {
>               ...
>       } else {
>               ...
>       }

Nothing's wrong with it exactly... It could be valid syntax. It seems
semantically odd though.

> Of course it brings other less wholesome things to mind like "elsfor"
> and "elsloop" and "if ... elsfor" and "for ... elsif ... elsloop ...
> else", but why not?

Urk. And why?

Besides, I would expect an C<elsfor> to actually be a loop of it's own,
on the principle of "elsif = else + if" so "elsfor = else + for".

> > And the condition for reaching the "else" is different too, it isn't
> > always a "false value",
>
> Yes it is.
> 
> > sometimes it's "no values to iterate over" or "condition met before
> > first execution of loop".
> 
>       while(@x) { ... }  /  if (@x) { ... }
>       while($x) { ... }  /  if ($x) { ... }
> 
> These are different how? To my eye, the only difference is what happens
> at the end of the block. In fact,
> 
>       while ($x) { ... ; last; }
> 
> is an if.

Yes, but there's a difference with C<while> in that you wouldn't want an
"else" to execute anytime the C<while> came across a false value, but
only if the *first* value it came across was false (i.e. only if the
loop isn't going to execute at all). Otherwise, you'd just use a LAST
block (a parallel that I think argues for ELSE blocks).

And C<for> and C<loop> are even more different. You could kind of
stretch the point and say that the evaluation of the second expression
is the false value, I'll give you that. But it still has the same
problem as C<while>.

  $i = $somevalue; # possibly set by user input to 5 or higher
  ...
  loop ; $i < 5; $i++ {
        ...
  }

And where's the "false value" here? The "else" would be more of "no
values to iterate over".

  for @names -> $name {
    ...
  }

> > I can also think of some advantages to having the "else" within the
> > scope of the loop. 
> 
>     while alllines("/etc/passwd") -> $_ {
>       ...
>     } else {
>       die "/etc/passwd: $_";
>     }

But the aliased value, $_, is restricted to the scope of the C<while>'s
block, it isn't going to be accessible in the block associated with the
C<else> (you would be getting some $_ from an outer scope). 

We could create exceptions to the scoping rules that would make it
possible. But part of the point in getting rid of C<continue> was to
avoid funky exceptions for code blocks dangling off the end of loops.

Allison

Reply via email to