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

Formatter: add braces when the iterator body is not enclosed #3122

Merged
merged 4 commits into from
Oct 25, 2024

Conversation

lukaszcz
Copy link
Collaborator

for (acc := 0) (x in lst) {
  x + acc
}
  • If the body is enclosed in ordinary parentheses, then they are preserved and the iterator is printed on a single line, if possible:
for (acc := 0) (x in lst) (x + acc)

This is sometimes useful when you want iterator application as an argument to something.

@lukaszcz lukaszcz added this to the 0.6.7 milestone Oct 23, 2024
@lukaszcz lukaszcz self-assigned this Oct 23, 2024
@lukaszcz lukaszcz changed the title Fromatter: add braces when the iterator body is not enclosed Formatter: add braces when the iterator body is not enclosed Oct 23, 2024
@janmasrovira
Copy link
Collaborator

It might be confusing to support both parentheses and braces with the same meaning. Should we always use parentheses?

@lukaszcz
Copy link
Collaborator Author

lukaszcz commented Oct 24, 2024

Yeah, maybe we should discuss this.

My idea was that the formatter behaves differently with braces v.s. parentheses. So you can use parentheses if you want to prevent the line break, but normally you would use braces. There are a few cases I noticed in the code where it actually makes sense to have iterator application as an argument, and then you don't want a line break. Other options are to use only braces and either try to detect when to do line breaks or always do line breaks (which results in poor formatting in the rare cases when you want iterator application as an argument).

Imho braces look better when something spans multiple lines -- they seem to indicate you have a "block" of something. We use them for this kind of grouping with e.g. case expressions.

@lukaszcz
Copy link
Collaborator Author

The way I thought about it: you either have an atom or a block enclosed in braces. Since an atom can be an expression enclosed in parentheses, that's where they come from.

For now, having an expression with multiple atoms for a body is still allowed, but the formatter will put braces around it and we should disallow it in the future.

@lukaszcz lukaszcz merged commit 3cf79fa into main Oct 25, 2024
4 checks passed
@lukaszcz lukaszcz deleted the iterator-braces branch October 25, 2024 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enforce braces around iterator bodies
2 participants