[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Should "continue" be dynamically or lexically scoped?
From: |
Andrew Janke |
Subject: |
Should "continue" be dynamically or lexically scoped? |
Date: |
Mon, 25 Mar 2019 17:27:26 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.6.0 |
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
- Should "continue" be dynamically or lexically scoped?,
Andrew Janke <=