-
Notifications
You must be signed in to change notification settings - Fork 1k
indentation explanation for scala 3 book #2445
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
Open
Shorla
wants to merge
4
commits into
scala:main
Choose a base branch
from
Shorla:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca8618c
Indentation for scala 2 reference made consistent as others
Shorla f811f39
Merge branch 'scala:main' into main
Shorla 2c5e88d
Optional Braces added and metadata fixed
Shorla 8d69289
Merge branch 'main' of github.com:Shorla/docs.scala-lang into main
Shorla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
title: Indentation | ||
type: section | ||
description: This section demonstrates Scala 3 Indentation. | ||
Shorla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
num: 8 | ||
previous-page: taste-vars-data-types | ||
Shorla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
next-page: taste-control-structures | ||
Shorla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--- | ||
|
||
Indentation refers to the increase or decrease of space between the left and right margin of a paragraph. | ||
In some programming languages, tab key is used to make indentations. This is not the case in Scala. Tab key automatically creates four spaces which Scala doesn't support. | ||
Shorla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
In Scala each level of indentation is 2 spaces. Thus, instead of | ||
Shorla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
indenting like this: | ||
|
||
// wrong! | ||
class Foo { | ||
def fourspaces = { | ||
val x = 4 | ||
.. | ||
} | ||
} | ||
|
||
You should indent like this: | ||
|
||
// right! | ||
class Foo { | ||
def twospaces = { | ||
val x = 2 | ||
.. | ||
} | ||
} | ||
|
||
The Scala language encourages a startling amount of nested scopes and | ||
logical blocks (function values and such). Do yourself a favor and don't | ||
penalize yourself syntactically for opening up a new block. Coming from | ||
Java, this style does take a bit of getting used to, but it is well | ||
worth the effort. | ||
|
||
## Line Wrapping | ||
|
||
There are times when a single expression reaches a length where it | ||
becomes unreadable to keep it confined to a single line (usually that | ||
length is anywhere above 80 characters). In such cases, the *preferred* | ||
approach is to simply split the expression up into multiple expressions | ||
by assigning intermediate results to values. However, this is not | ||
always a practical solution. | ||
|
||
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 | ||
have an unclosed parenthetical or to end with an infix method in which | ||
the right parameter is not given: | ||
|
||
val result = 1 + 2 + 3 + 4 + 5 + 6 + | ||
7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + | ||
15 + 16 + 17 + 18 + 19 + 20 | ||
|
||
Without this trailing method, Scala will infer a semi-colon at the end | ||
of a line which was intended to wrap, throwing off the compilation | ||
sometimes without even so much as a warning. | ||
|
||
## Methods with Numerous Arguments | ||
|
||
it is best practice to avoid any method which takes more than two or | ||
three parameters! When calling a method which takes numerous arguments (in the range of | ||
five or more),though, it is often necessary to wrap the method invocation onto | ||
multiple lines. In such cases, put each argument on a line by | ||
itself, indented two spaces from the current indent level: | ||
|
||
foo( | ||
someVeryLongFieldName, | ||
andAnotherVeryLongFieldName, | ||
"this is a string", | ||
3.1415) | ||
|
||
This way, all parameters line up, and you don't need to re-align them if | ||
you change the name of the method later on. | ||
|
||
Great care should be taken to avoid these sorts of invocations well into | ||
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: | ||
|
||
// right! | ||
val myLongFieldNameWithNoRealPoint = | ||
foo( | ||
someVeryLongFieldName, | ||
andAnotherVeryLongFieldName, | ||
"this is a string", | ||
3.1415) | ||
|
||
// wrong! | ||
val myLongFieldNameWithNoRealPoint = foo(someVeryLongFieldName, | ||
andAnotherVeryLongFieldName, | ||
"this is a string", | ||
3.1415) | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.