mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Now really adding some pattern matching
This commit is contained in:
parent
d346342ad6
commit
99b16c2672
@ -135,10 +135,29 @@ Person("George", "1234") == Person("Kate", "1236")
|
||||
|
||||
// Pattern matching
|
||||
|
||||
val me = Person("George", "1234")
|
||||
|
||||
me match { case Person(name, number) => "We matched someone : " + name + ", phone : " + number }
|
||||
|
||||
me match { case Person(name, number) => "Match : " + name; case _ => "Hm..." }
|
||||
|
||||
me match { case Person("George", number) => "Match"; case _ => "Hm..." }
|
||||
|
||||
me match { case Person("Kate", number) => "Match"; case _ => "Hm..." }
|
||||
|
||||
me match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" }
|
||||
|
||||
val kate = Person("Kate", "1234")
|
||||
|
||||
kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" }
|
||||
|
||||
|
||||
|
||||
// Regular expressions
|
||||
|
||||
|
||||
|
||||
|
||||
// Strings
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user