Boo

optional 'else' block for 'for' and 'while' loops

Details

  • Type: New Feature New Feature
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 0.8.1
  • Fix Version/s: 0.8.2
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

for <target_list> in <enumerable>:
   <for_block>
else:
   <else_block>

<else_block> is only executed when the loop terminates because <enumerable>.MoveNext() returns false when evaluated, i.e. whenever the <enumerable> attempts to move to the next item, but the <enumerable> is exhausted. This also means that if <enumerable> has no item at the first 'iteration', <enumerable>.MoveNext() will return false, and the <else_block> will be executed.


while <expression>:
     <while_block>
else:
     <else_block>

<else_block> is only executed when the loop terminates the <expression> is tested but resolves to a false, null, or 0 value. This also means that if <expression> is false the first time it is tested (even before entering <while_block>) <else_block> will be executed.

The effect is that for these loops, <else_block> will not be executed when execution leaves the loop due to a break, raise, or return because <expression>/<enumerable>.MoveNext() is not evaluated. Note that the cause of the <else-block> executing is the condition evaluating to false.

Issue Links

Activity

Hide
Marcus Griep added a comment -

This change should be minimally invasive, and as it adds to the language without adversely increasing complexity, I have no problems with it being implemented.

Show
Marcus Griep added a comment - This change should be minimally invasive, and as it adds to the language without adversely increasing complexity, I have no problems with it being implemented.
Hide
Marcus Griep added a comment - - edited

The current flow is equivalent to:

:looptarget
if condition:
  loop_block
  goto looptarget
else:
  else_block
:afterloop

There is discussion going on to change the flow to be equivalent to:

condMet = false
:looptarget
if condition:
  condMet = true
  loop_block
  goto looptarget
elif condMet:
  else_block
:afterloop

where a break in loop_block would evaluate to:

goto afterloop
Show
Marcus Griep added a comment - - edited The current flow is equivalent to:
:looptarget
if condition:
  loop_block
  goto looptarget
else:
  else_block
:afterloop
There is discussion going on to change the flow to be equivalent to:
condMet = false
:looptarget
if condition:
  condMet = true
  loop_block
  goto looptarget
elif condMet:
  else_block
:afterloop
where a break in loop_block would evaluate to:
goto afterloop

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: