From 47053f8cffc8a52969c35d9df8c53d39c5cef119 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Wed, 3 Mar 2021 16:23:28 -0800 Subject: [PATCH 01/13] Filling out template with PR 340 --- proposals/README.md | 1 + proposals/p0340.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 proposals/p0340.md diff --git a/proposals/README.md b/proposals/README.md index 6a97b8574240c..e8d96c4f6e584 100644 --- a/proposals/README.md +++ b/proposals/README.md @@ -59,5 +59,6 @@ request: - [0179 - Decision](p0179_decision.md) - [0198 - Comments](p0198.md) - [0199 - String literals](p0199.md) +- [0340 - while loops](p0340.md) diff --git a/proposals/p0340.md b/proposals/p0340.md new file mode 100644 index 0000000000000..9263342e07584 --- /dev/null +++ b/proposals/p0340.md @@ -0,0 +1,44 @@ +# while loops + + + +[Pull request](https://github.com/carbon-language/carbon-lang/pull/340) + + + +## Table of contents + +- [Problem](#problem) +- [Background](#background) +- [Proposal](#proposal) +- [Details](#details) +- [Alternatives considered](#alternatives-considered) + + + +## Problem + +TODO: What problem are you trying to solve? How important is that problem? Who +is impacted by it? + +## Background + +TODO: Is there any background that readers should consider to fully understand +this problem and your approach to solving it? + +## Proposal + +TODO: Briefly and at a high level, how do you propose to solve the problem? Why +will that in fact solve it? + +## Details + +TODO: Fully explain the details of the proposed solution. + +## Alternatives considered + +TODO: What alternative solutions have you considered? From 90411dae9685c7ba5d501e09b4e5bae6f2c4000d Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Wed, 3 Mar 2021 16:34:20 -0800 Subject: [PATCH 02/13] Porting from docs --- proposals/p0340.md | 181 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 173 insertions(+), 8 deletions(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 9263342e07584..e69347417ce49 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -16,29 +16,194 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [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) ## Problem -TODO: What problem are you trying to solve? How important is that problem? Who -is impacted by it? +`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 -TODO: Is there any background that readers should consider to fully understand -this problem and your approach to solving it? +- 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 -TODO: Briefly and at a high level, how do you propose to solve the problem? Why -will that in fact solve it? +Carbon should adopt `while` loop syntax consistent with C/C++. In particular: + +- `while`: used in both `while` and `do`/`while` loops. +- `do`: used in `do`/`while` loops. +- `continue`: continues with the next loop iteration, starting with the loop + condition. +- `break`: breaks out of the loop, without testing the loop condition. ## Details -TODO: Fully explain the details of the proposed solution. +Loop syntax looks like: + +- `while (` _boolean expression_ `) {` _statements_ `}` +- `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. + +`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 +%token CONTINUE +%token BREAK + +statement: + WHILE '(' expression ')' statement +| DO '(' expression ')' WHILE ';' +| 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 -TODO: What alternative solutions have you 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. + +### 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. From 6f377809660ecfb7530b78bfd7e38154a4e55680 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 4 Mar 2021 10:16:14 -0800 Subject: [PATCH 03/13] Be more explicit about optional braces --- proposals/p0340.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index e69347417ce49..5f7e7ec14b2c0 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -129,7 +129,8 @@ 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. +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, From 8e69514bd148fbbda2bc7cf7199442b213ca2cc9 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Mon, 15 Mar 2021 16:49:47 -0700 Subject: [PATCH 04/13] Responding to comments --- proposals/p0340.md | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 5f7e7ec14b2c0..b3f0b4e30e25b 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -19,9 +19,11 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [Executable semantics form](#executable-semantics-form) - [Caveats](#caveats) - [C++ as baseline](#c-as-baseline) + - [`do`/`while`](#dowhile) - [Alternatives considered](#alternatives-considered) - [`else` block](#else-block) - [Infinite loops](#infinite-loops) + - [Selection statements with initializer](#selection-statements-with-initializer) @@ -55,7 +57,7 @@ similar in many languages, even if details may change. while True: DoSomethingElse() - if !y: + if not y: break while z: @@ -109,7 +111,6 @@ similar in many languages, even if details may change. Carbon should adopt `while` loop syntax consistent with C/C++. In particular: - `while`: used in both `while` and `do`/`while` loops. -- `do`: used in `do`/`while` loops. - `continue`: continues with the next loop iteration, starting with the loop condition. - `break`: breaks out of the loop, without testing the loop condition. @@ -119,13 +120,10 @@ Carbon should adopt `while` loop syntax consistent with C/C++. In particular: Loop syntax looks like: - `while (` _boolean expression_ `) {` _statements_ `}` -- `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. +While will evaluate the loop condition before each 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), @@ -150,7 +148,6 @@ All of this is consistent with C/C++ behavior. statement: WHILE '(' expression ')' statement -| DO '(' expression ')' WHILE ';' | CONTINUE ';' | BREAK ';' | /* pre-existing statements elided */ @@ -175,6 +172,13 @@ 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. +### `do`/`while` + +`do`/`while` is omitted from this proposal because of disagreement about whether +it should be included in Carbon. It's better to have `do`/`while` considered +separately as a result, in order to separate review of the non-contentious +`while`. + ## Alternatives considered Both alternatives from the @@ -208,3 +212,26 @@ 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. + +### Selection statements with initializer + +[Selections statements with initializer](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html) +added `if (init; cond)` syntax to C++17. A corresponding `while (init; cond)` is +not proposed here, for several reasons: + +- Although P0305 added this syntax to `if` and `switch`, it does not appear to + be in `while`. Its addition in this proposal would be a divergence from C++ + which should probably be evaluated separately. + +- There is some disagreement about the syntax used. For example, P0305 + mentions a `with` keyword as an alternative, and there's some favor for + that. Particularly if Carbon omits the `for (init; cond; inc)` form that + `if (init; cond)` is structurally similar to, it may not make sense to do + `while (init; cond)`. + +- It's possible that `if ((var status_code c = bar()) != SUCCESS)` will be + valid in Carbon, solving some of the concerns that led to `if (init; cond)` + syntax in a different way. + +As a consequence, between not being in C++ and requiring more discussion, +`while (init; cond)` syntax is not part of this proposal. From 4dc0ca37f93aec92414da14f017b6f0701c2201e Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Wed, 17 Mar 2021 10:18:22 -0700 Subject: [PATCH 05/13] variable decl --- proposals/p0340.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/proposals/p0340.md b/proposals/p0340.md index b3f0b4e30e25b..915a46500ab3b 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -20,6 +20,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [Caveats](#caveats) - [C++ as baseline](#c-as-baseline) - [`do`/`while`](#dowhile) + - [Declaring variables inside control flow conditions](#declaring-variables-inside-control-flow-conditions) - [Alternatives considered](#alternatives-considered) - [`else` block](#else-block) - [Infinite loops](#infinite-loops) @@ -179,6 +180,30 @@ it should be included in Carbon. It's better to have `do`/`while` considered separately as a result, in order to separate review of the non-contentious `while`. +### Declaring variables inside control flow conditions + +C++ allows declaring a variable in the condition, such as: + +```cc +while (optional next = get_next()) { ... } +``` + +In Carbon, similar might be possible through: + +```carbon +while (var optional next = get_next()) { ... } +``` + +However, in Carbon even more advanced cases might work through expression +syntax: + +```carbon +while ((var Error next = get_next()).Success()) { ... } +``` + +This would be generic expression functionality, and has implications outside of +`while`. As a result, syntax specific to `while` is not part of this proposal. + ## Alternatives considered Both alternatives from the From a4ac33621b84fc41a9c325956ec3fd6cac2e7268 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Wed, 17 Mar 2021 11:04:51 -0700 Subject: [PATCH 06/13] words --- proposals/p0340.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 915a46500ab3b..76b5344f4a407 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -188,7 +188,7 @@ C++ allows declaring a variable in the condition, such as: while (optional next = get_next()) { ... } ``` -In Carbon, similar might be possible through: +In Carbon, similar might be possible through expression syntax: ```carbon while (var optional next = get_next()) { ... } From 8c4ea04666fd9a3c800b41e169009ff037d8a63a Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Mon, 29 Mar 2021 13:45:04 -0700 Subject: [PATCH 07/13] Rephrasing --- proposals/p0340.md | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 76b5344f4a407..872bca658461a 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -22,8 +22,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [`do`/`while`](#dowhile) - [Declaring variables inside control flow conditions](#declaring-variables-inside-control-flow-conditions) - [Alternatives considered](#alternatives-considered) - - [`else` block](#else-block) - - [Infinite loops](#infinite-loops) + - [Non-C++ syntax](#non-c-syntax) - [Selection statements with initializer](#selection-statements-with-initializer) @@ -213,30 +212,19 @@ The conclusions mirror here in order to avoid a divergence in syntax. Additional alternatives follow. -### `else` block +### Non-C++ syntax -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: +Various non-C++ features that came up and are not suggested by this proposal +because they aren't in C++ are: -``` -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. - -### Infinite loops +- `else` on `while`, as in Python. +- A `loop` statement with `while(true)` behavior, as in Rust. +- Labeled break and continue statements, as in Java or TypeScript. -`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. +These may be added later, but they are not part of the +[C++ baseline](#c-as-baseline), and have not received much consideration beyond +that adopting the proposed syntax would not significantly impair adoption of +such features. ### Selection statements with initializer From f8f35bf5e78aa6bff17b685d8047b69ec2531ee5 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Wed, 31 Mar 2021 15:12:03 -0700 Subject: [PATCH 08/13] Consolidate initialization comments to alternative --- proposals/p0340.md | 73 ++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 872bca658461a..58672344f201d 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -20,10 +20,9 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [Caveats](#caveats) - [C++ as baseline](#c-as-baseline) - [`do`/`while`](#dowhile) - - [Declaring variables inside control flow conditions](#declaring-variables-inside-control-flow-conditions) - [Alternatives considered](#alternatives-considered) - [Non-C++ syntax](#non-c-syntax) - - [Selection statements with initializer](#selection-statements-with-initializer) + - [Initializing variables in the `while`](#initializing-variables-in-the-while) @@ -179,30 +178,6 @@ it should be included in Carbon. It's better to have `do`/`while` considered separately as a result, in order to separate review of the non-contentious `while`. -### Declaring variables inside control flow conditions - -C++ allows declaring a variable in the condition, such as: - -```cc -while (optional next = get_next()) { ... } -``` - -In Carbon, similar might be possible through expression syntax: - -```carbon -while (var optional next = get_next()) { ... } -``` - -However, in Carbon even more advanced cases might work through expression -syntax: - -```carbon -while ((var Error next = get_next()).Success()) { ... } -``` - -This would be generic expression functionality, and has implications outside of -`while`. As a result, syntax specific to `while` is not part of this proposal. - ## Alternatives considered Both alternatives from the @@ -226,25 +201,39 @@ These may be added later, but they are not part of the that adopting the proposed syntax would not significantly impair adoption of such features. -### Selection statements with initializer +### Initializing variables in the `while` + +This proposal does not offer a way to initialize variables in the `while`. + +For comparison, C++ does allow declaring a variable in the condition, such as: + +```cc +while (optional next = get_next()) { ... } +``` +In addition, [Selections statements with initializer](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html) -added `if (init; cond)` syntax to C++17. A corresponding `while (init; cond)` is -not proposed here, for several reasons: +could be inferred to suggest a corresponding `while (init; cond)` syntax. -- Although P0305 added this syntax to `if` and `switch`, it does not appear to - be in `while`. Its addition in this proposal would be a divergence from C++ - which should probably be evaluated separately. +Neither of these is suggested in this proposal because we are likely to consider +a _different_ route of allowing declaration of a variable in expressions. For +example, the following would be legal not because `while` would use a +`condition` semantic that allows variable declarations as a form, but because it +uses `expression` semantics and `var` would be part of `expression` semantics: -- There is some disagreement about the syntax used. For example, P0305 - mentions a `with` keyword as an alternative, and there's some favor for - that. Particularly if Carbon omits the `for (init; cond; inc)` form that - `if (init; cond)` is structurally similar to, it may not make sense to do - `while (init; cond)`. +```carbon +while (var optional next = get_next()) { ... } +``` -- It's possible that `if ((var status_code c = bar()) != SUCCESS)` will be - valid in Carbon, solving some of the concerns that led to `if (init; cond)` - syntax in a different way. +In particular, this would also allow more flexible usage addressing more complex +use-cases that C++ does not, such as: + +```carbon +while ((var status_code c = bar()) != SUCCESS) { ... }` +``` -As a consequence, between not being in C++ and requiring more discussion, -`while (init; cond)` syntax is not part of this proposal. +This breaks slightly from the [C++ baseline](#c-as-baseline) by offering a +subset of C++ functionality. However, we can choose to add related functionality +later if `expression` semantics end up not including `var`. Temporarily omitting +`condition` functionality avoids having to reconcile it later if we pursue the +`expression` route, and it is not crucial to `while` loop functionality. From 81683432a72fa7c279ceedcd4906fb3b4a8d0e49 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 1 Apr 2021 14:30:38 -0700 Subject: [PATCH 09/13] Update proposals/p0340.md Co-authored-by: josh11b --- proposals/p0340.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 58672344f201d..3564321191254 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -107,7 +107,8 @@ similar in many languages, even if details may change. ## Proposal -Carbon should adopt `while` loop syntax consistent with C/C++. In particular: +Carbon should adopt `while` loop syntax consistent with C/C++. In particular, +it should adopt these three kinds of statements: - `while`: used in both `while` and `do`/`while` loops. - `continue`: continues with the next loop iteration, starting with the loop From 0fda165c332178d8f292b63488173e4ee232676b Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 1 Apr 2021 14:30:45 -0700 Subject: [PATCH 10/13] Update proposals/p0340.md Co-authored-by: josh11b --- proposals/p0340.md | 1 - 1 file changed, 1 deletion(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 3564321191254..a8ce30d1d1d2a 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -142,7 +142,6 @@ All of this is consistent with C/C++ behavior. ``` %token WHILE -%token DO %token CONTINUE %token BREAK From 7aed70cc12ee3ee1a0870f227051d662cd091f44 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 1 Apr 2021 14:32:55 -0700 Subject: [PATCH 11/13] rephrase --- proposals/p0340.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 58672344f201d..e3c031020b558 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -109,7 +109,7 @@ similar in many languages, even if details may change. Carbon should adopt `while` loop syntax consistent with C/C++. In particular: -- `while`: used in both `while` and `do`/`while` loops. +- `while`: declares that we're doing a loop, containing the condition. - `continue`: continues with the next loop iteration, starting with the loop condition. - `break`: breaks out of the loop, without testing the loop condition. From 2d14fcbcfdfc8a7726bb773f9c0a5eecd26fdfa4 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Tue, 13 Apr 2021 13:55:05 -0700 Subject: [PATCH 12/13] Rationale --- proposals/p0340.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index 024f9d0b69ca4..cb72de8dbc100 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -23,6 +23,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [Alternatives considered](#alternatives-considered) - [Non-C++ syntax](#non-c-syntax) - [Initializing variables in the `while`](#initializing-variables-in-the-while) +- [Rationale](#rationale) @@ -107,8 +108,8 @@ similar in many languages, even if details may change. ## Proposal -Carbon should adopt `while` loop syntax consistent with C/C++. In particular, -it should adopt these three kinds of statements: +Carbon should adopt `while` loop syntax consistent with C/C++. In particular, it +should adopt these three kinds of statements: - `while`: declares that we're doing a loop, containing the condition. - `continue`: continues with the next loop iteration, starting with the loop @@ -237,3 +238,12 @@ subset of C++ functionality. However, we can choose to add related functionality later if `expression` semantics end up not including `var`. Temporarily omitting `condition` functionality avoids having to reconcile it later if we pursue the `expression` route, and it is not crucial to `while` loop functionality. + +## Rationale + +- C++'s `while` loop style is common and desirable to stay consistent with. +- Initializing variables in the `while` is not immediately necessary to have + `while` be useful; we may even choose to omit it. It's not necessary to + decide immediately. + - Some discussion about this is in + [issue #425](https://github.com/carbon-language/carbon-lang/issues/425). From 1be96884e4478c5b53b42e4bee3fd11b1f1b5344 Mon Sep 17 00:00:00 2001 From: jonmeow <46229924+jonmeow@users.noreply.github.com> Date: Tue, 20 Apr 2021 09:00:21 -0700 Subject: [PATCH 13/13] Updates --- proposals/p0340.md | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/proposals/p0340.md b/proposals/p0340.md index cb72de8dbc100..b68de7583ad94 100644 --- a/proposals/p0340.md +++ b/proposals/p0340.md @@ -20,10 +20,10 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [Caveats](#caveats) - [C++ as baseline](#c-as-baseline) - [`do`/`while`](#dowhile) +- [Rationale based on Carbon's goals](#rationale-based-on-carbons-goals) - [Alternatives considered](#alternatives-considered) - [Non-C++ syntax](#non-c-syntax) - [Initializing variables in the `while`](#initializing-variables-in-the-while) -- [Rationale](#rationale) @@ -179,6 +179,19 @@ it should be included in Carbon. It's better to have `do`/`while` considered separately as a result, in order to separate review of the non-contentious `while`. +## Rationale based on Carbon's goals + +Relevant goals are: + +- [3. Code that is easy to read, understand, and write](/docs/project/goals.md#code-that-is-easy-to-read-understand-and-write): + + - `while` loops are easy to read and very helpful. + +- [7. Interoperability with and migration from existing C++ code](/docs/project/goals.md#interoperability-with-and-migration-from-existing-c-code): + + - Keeping syntax close to C++ will make it easier for developers to + transition. + ## Alternatives considered Both alternatives from the @@ -238,12 +251,3 @@ subset of C++ functionality. However, we can choose to add related functionality later if `expression` semantics end up not including `var`. Temporarily omitting `condition` functionality avoids having to reconcile it later if we pursue the `expression` route, and it is not crucial to `while` loop functionality. - -## Rationale - -- C++'s `while` loop style is common and desirable to stay consistent with. -- Initializing variables in the `while` is not immediately necessary to have - `while` be useful; we may even choose to omit it. It's not necessary to - decide immediately. - - Some discussion about this is in - [issue #425](https://github.com/carbon-language/carbon-lang/issues/425).