-
Notifications
You must be signed in to change notification settings - Fork 1k
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
indentation explanation for scala 3 book #2445
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for opening a PR @Shorla.
I am a bit confused, however, because I thought you would introduce the fact that the indentation is significant in Scala 3, but you mostly talk about code style best practices. I would expect to see a discussion about braces being optional, as explained here: https://docs.scala-lang.org/scala3/reference/other-new-features/indentation.html
@bishabosha I made the adjustments for the metadata, but I'm yet to push it because I'm trying to conclude on the article. But I should push the full changes before the end of the week. |
Ok great :), let us know if you want any more reviews |
hi @bishabosha @julienrf, I couldn't add all of the information on optional braces. I added a part then a link to the Scala 3 reference article. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some structural notes.
next-page: taste-control-structures | ||
--- | ||
|
||
Indentation refers to the increase or decrease of space between the left and right margin of a paragraph. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a cogent sentence.
class Foo { | ||
def fourspaces = { | ||
val x = 4 | ||
.. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if there is a local style for "omitted code" and whether it is styled, but there ought to be.
} | ||
} | ||
|
||
The Scala language encourages a startling amount of nested scopes and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Users accustomed to Java might be startled by how deeply constructs can be nested in Scala."
The "Coming from Java" should come first. The syntax itself is not startling per se.
|
||
When it is absolutely necessary to wrap an expression across more than | ||
one line, each successive line should be indented two spaces from the | ||
*first*. Also remember that Scala requires each "wrap line" to either |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scala 3 or Scala 2 -Xsource:3 supports leading infix operator on the next line.
I'd suggest presenting the problematic code and then the solution (scalafmt or whatever).
|
||
val result = 1 + 2 + 3 + 4 + 5 + 6 + | ||
7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + | ||
15 + 16 + 17 + 18 + 19 + 20 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a verbose snippet
|
||
## Methods with Numerous Arguments | ||
|
||
it is best practice to avoid any method which takes more than two or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"It". A paragraph about usual styles of method invocation would be helpful, including when to use named args (such as with boolean constants, f(recurse = true)
). It would be most succinct to refer to a scalafmt config and show the output.
the length of the line. More specifically, such an invocation should be | ||
avoided when each parameter would have to be indented more than 50 | ||
spaces to achieve alignment. In such cases, the invocation itself should | ||
be moved to the next line and indented two spaces: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This happens often. It's very annoying. There is no good solution except scalafmt, which I don't use, but I'd like to see the config that will format to some standard, and also see an example. I'm not likely to memorize rules.
- An `<outdent>` is also inserted if the next token following a statement sequence starting with an `<indent>` closes an indentation region, i.e. is one of `then`, `else`, `do`, `catch`, `finally`, `yield`, `}`, `)`, `]` or `case`. | ||
|
||
- An `<outdent>` is finally inserted in front of a comma that follows a statement sequence starting with an `<indent>` if the indented region is itself enclosed in parentheses. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The preceding is more like spec. I can't review that ATM, but I think some guidance about "when do I need to think about indentation" would be worthwhile. That is, normally, it should just work. When does it not just work?
This solves the issue at #2266