Should "continue" be dynamically or lexically scoped?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Should "continue" be dynamically or lexically scoped?

apjanke-floss
Hi folks,

A somewhat philosophical language design question for you: Should
while/for loops' "continue" be dynamically or lexically scoped? How
about "break"?

I asked this on the bug tracker at https://savannah.gnu.org/bugs/?55995 
and Rik sent me over here to get a bigger audience.

The current situation is this: continue is dynamically scoped, but break
is lexically scoped. This means that a break can only be used inside the
text of an actual for/while loop, in the same function that defines it.
But you can call continue anywhere, and it will look up the function
call stack and affect the execution of a for/while loop in a calling
function.

Example:

# looper.m
function looper
   for i = 1:5
     fprintf ("before\n");
     other_fcn ();
     fprintf ("after\n");
   end
end

#other_fcn.m
function other_fcn
   continue
end

Output:
 >> looper
before
before
before
before
before
 >> continue
 >> continue
 >> continue
 >> other_fcn
 >> other_fcn
 >>

But you can't call "break" this way. That's a syntax error.

 >> break
parse error:

   break must appear in a loop in the same file as loop command

 >>> break
         ^

 >> call_break
parse error near line 2 of file
/Users/janke/tmp/octave-continue/call_break.m

   break must appear in a loop in the same file as loop command

 >>>   break
           ^

 >>

Personally, I lean towards "continue" being lexically scoped, so you can
always see where it happens while you're looking at the source code of
the function that contains the for loop. Non-local affects on control
structures are harder to understand and debug. (This is why some
developers dislike exceptions.)

Cheers,
Andrew

Reply | Threaded
Open this post in threaded view
|

Re: Should "continue" be dynamically or lexically scoped?

Rik-4
On 03/25/2019 02:27 PM, [hidden email] wrote:
Subject:
Should "continue" be dynamically or lexically scoped?
From:
Andrew Janke [hidden email]
Date:
03/25/2019 02:27 PM
To:
octave-maintainers [hidden email]
List-Post:
[hidden email]
Content-Transfer-Encoding:
8bit
Precedence:
list
MIME-Version:
1.0
Message-ID:
[hidden email]
Content-Type:
text/plain; charset=utf-8; format=flowed
Message:
5

Hi folks,

A somewhat philosophical language design question for you: Should while/for loops' "continue" be dynamically or lexically scoped? How about "break"?

I asked this on the bug tracker at https://savannah.gnu.org/bugs/?55995 and Rik sent me over here to get a bigger audience.

The current situation is this: continue is dynamically scoped, but break is lexically scoped. This means that a break can only be used inside the text of an actual for/while loop, in the same function that defines it. But you can call continue anywhere, and it will look up the function call stack and affect the execution of a for/while loop in a calling function.


Personally, I lean towards "continue" being lexically scoped, so you can always see where it happens while you're looking at the source code of the function that contains the for loop. Non-local affects on control structures are harder to understand and debug. (This is why some developers dislike exceptions.)

I tend to agree that it makes understanding flow control very difficult.  As another modest argument in its favor, Matlab supports lexical rather than dynamic scoping for "continue".  On the other hand, there are languages, like Perl which I tested, which do allow this.  I think it makes even less sense though to have "break" be lexically scoped and "continue" be dynamically scoped.  It seems like the loop keywords should at least all be treated in the same fashion.

--Rik