Skip to content

Commit 6a26fec

Browse files
authored
Merge pull request #749 from travissarles/type-bounds
Rewrote upper type bounds article
2 parents d915d25 + b174c9e commit 6a26fec

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Diff for: tutorials/tour/_posts/2017-02-13-upper-type-bounds.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@ class Lion extends Animal {
3333
override def name: String = "Lion"
3434
}
3535
36-
class Cage[P <: Pet](p: P) {
36+
class PetContainer[P <: Pet](p: P) {
3737
def pet: P = p
3838
}
3939
40-
object Main extends App {
41-
var dogCage = new Cage[Dog](new Dog)
42-
var catCage = new Cage[Cat](new Cat)
43-
/* Cannot put Lion in a cage as Lion is not a Pet. */
44-
// var lionCage = new Cage[Lion](new Lion)
45-
}
40+
val dogContainer = new PetContainer[Dog](new Dog)
41+
val catContainer = new PetContainer[Cat](new Cat)
42+
// val lionContainer = new PetContainer[Lion](new Lion)
43+
// ^this would not compile
4644
```
45+
The `class PetContainer` take a type parameter `P` which must be a subtype of `Pet`. `Dog` and `Cat` are subtypes of `Pet` so we can create a new `PetContainer[Dog]` and `PetContainer[Cat]`. However, if we tried to create a `PetContainer[Lion]`, we would get the following Error:
4746

48-
An instance of class `Cage` may contain an animal with upper bound `Pet`. An animal of type `Lion` is not a pet and therefore cannot be put into a cage.
47+
`type arguments [Lion] do not conform to class PetContainer's type parameter bounds [P <: Pet]`
4948

50-
The usage of lower type bounds is discussed [here](lower-type-bounds.html).
49+
This is because `Lion` is not a subtype of `Pet`.

0 commit comments

Comments
 (0)