more compact formatting for while-loops with no body#2812
Open
v-gb wants to merge 1 commit into
Open
Conversation
Collaborator
|
Not sure why the CI is failing, but I agree with the goal. The tests and implementation are good. |
EmileTrotignon
approved these changes
Jun 29, 2026
Concretely, this is what changes:
```diff
while
loooong
condition
-do
- ()
-done
+do () done
```
A real example where I wished for a more compact formatting:
ocaml/ocaml-re@59198dd#diff-a91a869b078e8995843c8b377da588505468c962166d62d7029893c27be32833R70-R80
To explain a bit: while-loops are fairly inexpressive in ocaml due to the absence of
`break`. In a decent number of cases, it's nicer to give up on the separation between
condition and body, by moving the body into the condition. In that case, it's a bit
silly to waste 3 lines on the empty body.
For instance, if you wanted to write a loop where the first iteration always runs, like
a do-while in C, the obvious thing to do (without auxiliary functions) is:
```ocaml
let first = ref true in
while !first || condition; do first := false; body done
```
but the simpler version is:
```ocaml
while body; condition do () done
```
Or iterating through a stack:
```ocaml
while not (Stack.is_empty s) do f (Stack.pop_exn s) done
```
can be done without double-checking that the stack is empty:
```ocaml
while
match Stack.pop s with
| None -> false
| Some elt -> f elt; true
do () done
```
Contributor
Author
|
Thanks! Right, the failures are unrelated to the change, though no idea either why they are occurring. I added the PR number to the CHANGES file (it's going to incidentally retrigger builds, but I expect they will fail again). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Concretely, this is what changes:
while loooong condition -do - () -done +do () doneA real example where I wished for a more compact formatting:
ocaml/ocaml-re@59198dd#diff-a91a869b078e8995843c8b377da588505468c962166d62d7029893c27be32833R70-R80
To explain a bit: while-loops are fairly inexpressive in ocaml due to the absence of
break. In a decent number of cases, it's nicer to give up on the separation between condition and body, by moving the body into the condition. In that case, it's a bit silly to waste 3 lines on the empty body.For instance, if you wanted to write a loop where the first iteration always runs, like a do-while in C, the obvious thing to do (without auxiliary functions) is:
but the simpler version is:
Or iterating through a stack:
can be done without double-checking that the stack is empty: