10000 Examples for binding patterns to variables by gkepka · Pull Request #3071 · scala/docs.scala-lang · GitHub
[go: up one dir, main page]

Skip to content

Examples for binding patterns to variables #3071

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 2 commits into from
Oct 26, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Fix double declaration of classes and functions
  • Loading branch information
gkepka committed Sep 15, 2024
commit 8ec1d34527adb813140a26254b47510ffddcf3aa
20 changes: 2 additions & 18 deletions _tour/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,31 +242,15 @@ You can use variable binding to get type-dependent behavior while simultaneously
{% tabs pattern-matching-variable-binding class=tabs-scala-version %}
{% tab 'Scala 2' for=pattern-matching-variable-binding %}
```scala mdoc
sealed trait Device
case class Phone(model: String) extends Device {
def screenOff = "Turning screen off"
}
case class Computer(model: String) extends Device {
def screenSaverOn = "Turning screen saver on..."
}

def goIdle(device: Device): String = device match {
def goIdleWithModel(device: Device): String = device match {
case p @ Phone(model) => s"$model: ${p.screenOff}"
case c @ Computer(model) => s"$model: ${c.screenSaverOn}"
}
```
{% endtab %}
{% tab 'Scala 3' for=pattern-matching-variable-binding %}
```scala
sealed trait Device
case class Phone(model: String) extends Device:
def screenOff = "Turning screen off"

case class Computer(model: String) extends Device:
def screenSaverOn = "Turning screen saver on..."


def goIdle(device: Device): String = device match
def goIdleWithModel(device: Device): String = device match
case p @ Phone(model) => s"$model: ${p.screenOff}"
case c @ Computer(model) => s"$model: ${c.screenSaverOn}"
```
Expand Down
0