Skip to content

Present scala 2 if syntax #2284

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

Merged
merged 4 commits into from
Jan 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion _overviews/scala3-book/taste-control-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ As you’ll see throughout this book, _all_ Scala control structures can be used
> An expression returns a result, while a statement does not.
> Statements are typically used for their side-effects, such as using `println` to print to the console.

The `if`/`else` control struture in Scala 2 was constructed differently, with parentheses required and curly brackets instead of the keyword `then`.

```scala
// Scala 2 syntax
if (test1) {
doX()
} else if (test2) {
doY()
} else {
doZ()
}
```

## `for` loops and expressions

Expand All @@ -60,12 +71,15 @@ for i <- ints do println(i)

The code `i <- ints` is referred to as a _generator_, and the code that follows the `do` keyword is the _body_ of the loop.

The old syntax for this control structure was:
The old syntax in Scala 2 for this control structure was:

```scala
// Scala 2 syntax
for (i <- ints) println(i)
```

Again, note the usage of parentheses and the new `for`-`do` in Scala 3.

### Guards

You can also use one or more `if` expressions inside a `for` loop.
Expand Down