evhub / evhub/coconut

Feature: labeled `break` and `continue` statements

Open
#659 0 comments 0 reactions 0 assignees View on GitHub
feature
Dominant language
Python
Stars
4.4k
Forks
142
PR merge metrics
No merged PRs in 30d

Description

# Are there plans to add support for labeled `for`, `while`, `break`, and `continue` statements?

## Reasons

This feature is primarily intended for correctness' sake, but also convenience.

**Correctness**: Say I'm refactoring:

```python
while ...:
# big code block 1
for ...:
# big code block 2
# big code block 3
break
# big code block 4
```

into something like

```python
while ...:
# big code block 1
# big code block 3
break
# big code block 4
```

The `break` statement was intended to only apply to the inner loop, but amidst the KLOC's I accidently left the break statement inside the outer loop. This is not correct. Labeled break's would prevent this issue.

**Convenience**: I often write small loops to check any possible valid condition and apply this check to an entire sequence. Usually, simplicity requires lifting the entire nested loops into their own function. For example,

```python
"""Checks if a board state ever had a 1 adjacent to the player"""

def readable_check(trajectory):
for pos, board in trajectory:
for dir in [N, S, E, W]:
if board[pos + dir] == 1:
return True
return False

trajectory = ...
# check if player was ever touching a 1
check = readable_check(trajectory) # readable, not concise, not inline
check = any(board[pos+dir] == 1 for board in trajectory for pos in [N, S, E, W]) # concise, not super readable, inline

# readable, not concise, inline
check: bool
for pos, board in trajectory:
for dir in [N, S, E, W]:
if board[pos + dir] == 1:
check = True
break
if check:
break
else:
check = False
```

Maybe it's just my opinion, but I think labeled `break`s and `continue`s would be a more pleasant way to express myself here:

```python
# readable, reasonably-concise, inline
check: bool
trajectory_loop:
for pos, board in trajectory:
positions_loop:
for dir in [N, S, E, W]:
if board[pos + dir] == 1:
check = True
break trajectory_loop
else:
check = False
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.