Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CS-206 Demos
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sankalp Gambhir
CS-206 Demos
Commits
d84371c3
Commit
d84371c3
authored
1 year ago
by
Matt Bovel
Browse files
Options
Downloads
Patches
Plain Diff
Create 39368.worksheet.sc
parent
74514251
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/scala/ed/39368.worksheet.sc
+22
-0
22 additions, 0 deletions
src/main/scala/ed/39368.worksheet.sc
with
22 additions
and
0 deletions
src/main/scala/ed/39368.worksheet.sc
0 → 100644
+
22
−
0
View file @
d84371c3
//
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
"
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment