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

Add documentation for Pattern Matching on String #3118

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Changes from 3 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
63 changes: 63 additions & 0 deletions _tour/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,69 @@ println(showNotification(someVoiceRecording)) // prints You received a Voice Re

The function `showNotification` takes as a parameter the abstract type `Notification` and matches on the type of `Notification` (i.e. it figures out whether it's an `Email`, `SMS`, or `VoiceRecording`). In the `case Email(sender, title, _)` the fields `sender` and `title` are used in the return value but the `body` field is ignored with `_`.

## Matching on string

The `s`-interpolator allows embedding variables in strings and is also useful for pattern matching.

{% tabs s-interpolator-pattern-matching class=tabs-scala-version %}
{% tab 'Scala 2' for=s-interpolator-pattern-matching %}
```scala
val input: String = "Alice is 25 years old"

input match {
case s"$name is $age years old" => s"$name's age is $age"
case _ => "No match"
}
// Result: "Alice's age is 25"
```
{% endtab %}
{% tab 'Scala 3' for=s-interpolator-pattern-matching %}
```scala
val input: String = "Alice is 25 years old"

input match
case s"$name is $age years old" => s"$name's age is $age"
case _ => "No match"
// Result: "Alice's age is 25"
```
{% endtab %}
{% endtabs %}

In this example, name and age extract parts of the string based on the pattern. This is helpful for parsing structured text.

We can also use extractor objects for string pattern matching.

{% tabs s-interpolator-pattern-matching-2 class=tabs-scala-version %}
{% tab 'Scala 2' for=s-interpolator-pattern-matching-2 %}
```scala
object Int {
def unapply(s: String): Option[Int] = s.toIntOption
}
Friendseeker marked this conversation as resolved.
Show resolved Hide resolved

val input: String = "Alice is 25 years old"

val (name, age) = input match {
case s"$name is ${Int(age)} years old" => (name, age)
}
// name: String = Alice
// age: Int = 25
```
{% endtab %}
{% tab 'Scala 3' for=s-interpolator-pattern-matching-2 %}
```scala
object Int:
def unapply(s: String): Option[Int] = s.toIntOption

val input: String = "Alice is 25 years old"

val (name, age) = input match
case s"$name is ${Int(age)} years old" => (name, age)
// name: String = Alice
// age: Int = 25
```
{% endtab %}
{% endtabs %}

## Pattern guards
Pattern guards are boolean expressions which are used to make cases more specific. Just add `if <boolean expression>` after the pattern.

Expand Down
Loading