Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C++-like while loops #340

Merged
merged 17 commits into from
Apr 21, 2021
1 change: 1 addition & 0 deletions proposals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ request:
- [0179 - Decision](p0179_decision.md)
- [0198 - Comments](p0198.md)
- [0199 - String literals](p0199.md)
- [0340 - while loops](p0340.md)

<!-- endproposals -->
210 changes: 210 additions & 0 deletions proposals/p0340.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# while loops

<!--
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->

[Pull request](https://github.com/carbon-language/carbon-lang/pull/340)

<!-- toc -->

## Table of contents

- [Problem](#problem)
- [Background](#background)
- [Proposal](#proposal)
- [Details](#details)
- [Executable semantics form](#executable-semantics-form)
- [Caveats](#caveats)
- [C++ as baseline](#c-as-baseline)
- [Alternatives considered](#alternatives-considered)
- [`else` block](#else-block)
- [Infinite loops](#infinite-loops)

<!-- tocstop -->

## Problem

`while` is noted in the [language overview](/docs/design/README.md#while), but
is provisional. Control flow is important, and `while` is basic; the form is
similar in many languages, even if details may change.

## Background

- C++: A couple example languages following C++'s syntax closely are Java and
TypeScript.

```cc
while (x) {
DoSomething();
}

do {
DoSomethingElse();
} while (y);
```

- Python: Python does not provide `do`/`while`. However, the `else` syntax for
having code execute if the condition is _never_ true may be of interest.

```python
while x:
DoSomething()

while True:
DoSomethingElse()
if !y:
break

while z:
print("z is true")
else:
print("z was never true")
```

- Swift: Swift uses `repeat` instead of `do`.

```swift
while x {
DoSomething()
}

repeat {
DoSomethingElse()
} while y
```

- Rust: Rust provides only a basic `while` loop, relying on the condition-less
`loop` to achieve `do`/`while`-like behavior.

```rust
while x {
DoSomething();
}

loop {
DoSomethingElse();
if (!y) { break; }
}
```

- Go: Go has no `while` loops, only `for` loops. However, a `for` can be
written similar to a `while`.

```go
for x {
DoSomething()
}

for {
DoSomethingElse();
if !y { break; }
}
```

## Proposal

Carbon should adopt `while` loop syntax consistent with C/C++. In particular:
jonmeow marked this conversation as resolved.
Show resolved Hide resolved

- `while`: used in both `while` and `do`/`while` loops.
austern marked this conversation as resolved.
Show resolved Hide resolved
- `do`: used in `do`/`while` loops.
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
- `continue`: continues with the next loop iteration, starting with the loop
condition.
- `break`: breaks out of the loop, without testing the loop condition.

## Details

Loop syntax looks like:

- `while (` _boolean expression_ `) {` _statements_ `}`
austern marked this conversation as resolved.
Show resolved Hide resolved
austern marked this conversation as resolved.
Show resolved Hide resolved
- `do {` _statements_ `} while (` _boolean expression_ `);`

`while` and `do`/`while` only differ on the first run of the loop: `while` will
evaluate the loop condition (the boolean expression) first, `do`/`while` will
not. Both will evaluate the loop condition before each additional pass of the
loop, only continuing if the loop condition is true. When the loop condition
evaluates to false, the loop completes.

Similar to the
[`if`/`else` proposal](https://github.com/carbon-language/carbon-lang/pull/285),
the braces are optional and must be paired (`{ ... }`) if present. When there
are no braces, only one statement is allowed.

`continue` will continue with the next loop iteration directly, skipping any
other statements in the loop body. The next loop iteration behaves as normal,
starting with the condition being tested.

`break` exits the loop immediately, without testing the condition.

All of this is consistent with C/C++ behavior.

### Executable semantics form

```
%token WHILE
%token DO
jonmeow marked this conversation as resolved.
Show resolved Hide resolved
%token CONTINUE
%token BREAK

statement:
WHILE '(' expression ')' statement
| DO '(' expression ')' WHILE ';'
chandlerc marked this conversation as resolved.
Show resolved Hide resolved
| CONTINUE ';'
| BREAK ';'
| /* pre-existing statements elided */
;
```

Note that `continue` and `break` should only be valid in a loop context.

## Caveats

### C++ as baseline

This baseline syntax is based on C++, following the migration sub-goal
[Familiarity for experienced C++ developers with a gentle learning curve](/docs/project/goals.md#interoperability-with-and-migration-from-existing-c-code).
To the extent that this proposal anchors on a particular approach, it aims to
anchor on C++'s existing syntax, consistent with that sub-goal.

Alternatives will generally reflect breaking consistency with C++ syntax. While
most proposals may consider alternatives more, this proposal suggests a
threshold of only accepting alternatives that skew from C++ syntax if they are
clearly better; the priority in this proposal is to _avoid debate_ and produce a
trivial proposal. Where an alternative would trigger debate, it should be
examined by an advocate in a separate proposal.

## Alternatives considered

Both alternatives from the
[`if`/`else` proposal](https://github.com/carbon-language/carbon-lang/pull/285)
apply to `while` as well: we could remove parentheses, require braces, or both.
The conclusions mirror here in order to avoid a divergence in syntax.

Additional alternatives follow.

### `else` block

Some languages support an `else` following `while`. This can support, for
example, running logic if the `while` condition never evaluated true, and so the
typical loop body has never run. For example, in Python:

```
while x:
print("x is true")
else:
print("x was *never* true")
```

This may be added later, but is not part of this proposal because it is not part
of C++ syntax.
chandlerc marked this conversation as resolved.
Show resolved Hide resolved

### Infinite loops

`while (true)` is a common idiom in C++ for an infinite loop. It may be useful
to add dedicated syntax for this, such as Rust's `loop`. However, this may be
addressed as part of a more generic looping solution that better addresses
complex situations (such as advanced use of C++'s `for (...; ...; ...)`).
Regardless, `loop` is not part of C++ syntax and so we should be cautious about
adding it.