From d84371c304725641cb48a078543b7b4e378b53f1 Mon Sep 17 00:00:00 2001
From: Matt Bovel <matthieu@bovel.net>
Date: Fri, 26 May 2023 16:17:13 +0200
Subject: [PATCH] Create 39368.worksheet.sc

---
 src/main/scala/ed/39368.worksheet.sc | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 src/main/scala/ed/39368.worksheet.sc

diff --git a/src/main/scala/ed/39368.worksheet.sc b/src/main/scala/ed/39368.worksheet.sc
new file mode 100644
index 0000000..83bfddf
--- /dev/null
+++ b/src/main/scala/ed/39368.worksheet.sc
@@ -0,0 +1,22 @@
+// To demonstrate different ways of pattern matching, let's consider the
+// following example case class and instance:
+case class Person(name: String, age: Int)
+val ada = Person("Ada", 36)
+
+// There are several ways to pattern match on the `ada` instance:
+// 1. Pattern matching on the case class constructor using `Person(name, age)`.
+//    If the pattern matches, the value of `n` is bound `ada.name` field, and
+//    `a` is bound to the `ada.age` field.
+ada match
+  case Person(n, a) => s"$n is $a years old"
+
+// 2. We can also check only that `ada` is of type `Person` without binding its
+//    fields by using a `:` pattern. The `:` pattern is used to check if a value is
+//    of a certain type.
+ada match
+  case p: Person => s"${p.name} is ${p.age} years old"
+
+// 3. If we want to both bind the fields and bind a value to the whole instance,
+//    we can use an `@` pattern.
+ada match
+  case p @ Person(n, a) => s"${p.name} is ${a} years old"
-- 
GitLab