Skip to content

Commit 8e54cdc

Browse files
committed
clarified the constructor matching code
1 parent 9b2ea96 commit 8e54cdc

3 files changed

Lines changed: 61 additions & 3 deletions

File tree

src/main/scala/s4j/scala/chapter18/BasicConstructorPatternExample.scala renamed to src/main/scala/s4j/scala/chapter18/BasicConstructorPatternExample1.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
package s4j.scala.chapter18
1818

19-
object BasicConstructorPatternExample extends App {
19+
object BasicConstructorPatternExample1 extends App {
2020

2121
val hero = new SuperHero("Batman", "Bruce Wayne", List("Speed", "Agility", "Strength"))
2222

2323
hero match {
2424
case SuperHero(_, "Bruce Wayne", _) => println("I'm Batman!")
25-
case SuperHero(_, _, _) => println("I'm Batman!")
26-
case _ => println("I'm a civilian")
25+
case SuperHero(_, _, _) => println("???")
2726
}
2827

2928
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2015-2017 Toby Weston
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package s4j.scala.chapter18
18+
19+
object BasicConstructorPatternExample2 extends App {
20+
21+
val hero = new Person("Joe Ordinary")
22+
23+
// produces a 'MatchError' as a Person doesn't match anything
24+
hero match {
25+
case SuperHero(_, "Bruce Wayne", _) => println("I'm Batman!")
26+
case SuperHero(_, _, _) => println("???")
27+
}
28+
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2015-2017 Toby Weston
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package s4j.scala.chapter18
18+
19+
object BasicConstructorPatternExample3 extends App {
20+
21+
val hero = new Person("Joe Ordinary")
22+
23+
// adding the wildcard _ means Joe matches the last case
24+
hero match {
25+
case SuperHero(_, "Bruce Wayne", _) => println("I'm Batman!")
26+
case SuperHero(_, _, _) => println("???")
27+
case _ => println("I'm a civilian")
28+
}
29+
30+
}

0 commit comments

Comments
 (0)