Skip to content

Commit

Permalink
Add match following a period example and remove languages from Partia…
Browse files Browse the repository at this point in the history
…l Functions page
  • Loading branch information
gkepka committed Dec 10, 2024
1 parent 3d80ecd commit 38ac7a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 15 additions & 3 deletions _overviews/scala3-book/control-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -1040,15 +1040,27 @@ match
{% endtab %}
{% endtabs %}

The `match` expression can also follow a period, which simplifies matching on results returned by chained method calls:

{% tabs 'control-structures-33' %}
{% tab 'Scala 3 Only' %}
```scala
List(1, 2, 3).map(_ * 2).headOption.match
case Some(value) => println(s"The head is: $value")
case None => println("The list is empty")
```
{% endtab %}
{% endtabs %}

## try/catch/finally

Like Java, Scala has a `try`/`catch`/`finally` construct to let you catch and manage exceptions.
For consistency, Scala uses the same syntax that `match` expressions use and supports pattern matching on the different possible exceptions that can occur.

In the following example, `openAndReadAFile` is a method that does what its name implies: it opens a file and reads the text in it, assigning the result to the mutable variable `text`:

{% tabs control-structures-33 class=tabs-scala-version %}
{% tab 'Scala 2' for=control-structures-33 %}
{% tabs control-structures-34 class=tabs-scala-version %}
{% tab 'Scala 2' for=control-structures-34 %}
```scala
var text = ""
try {
Expand All @@ -1062,7 +1074,7 @@ try {
}
```
{% endtab %}
{% tab 'Scala 3' for=control-structures-33 %}
{% tab 'Scala 3' for=control-structures-34 %}
```scala
var text = ""
try
Expand Down
1 change: 0 additions & 1 deletion _overviews/scala3-book/fun-partial-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: Partial Functions
type: section
description: This page shows how to use function variables in Scala.
languages: [ru, zh-cn]
num: 31
previous-page: fun-function-variables
next-page: fun-eta-expansion
Expand Down

0 comments on commit 38ac7a3

Please sign in to comment.