Skip to content

Commit 57e659f

Browse files
committed
Changed type from T to B for consistency
1 parent 1ec7d50 commit 57e659f

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

tutorials/tour/_posts/2017-02-13-lower-type-bounds.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,44 @@ previous-page: upper-type-bounds
1212
prerequisite-knowledge: upper-type-bounds, generics, variance
1313
---
1414

15-
While [upper type bounds](upper-type-bounds.html) limit a type to a subtype of another type, *lower type bounds* declare a type to be a supertype of another type. The term `T >: A` expresses that the type parameter `T` or the abstract type `T` refer to a supertype of type `A`. In most cases, `A` will be the type parameter of the class and `T` will be the type parameter of a method.
15+
While [upper type bounds](upper-type-bounds.html) limit a type to a subtype of another type, *lower type bounds* declare a type to be a supertype of another type. The term `B >: A` expresses that the type parameter `B` or the abstract type `B` refer to a supertype of type `A`. In most cases, `A` will be the type parameter of the class and `B` will be the type parameter of a method.
1616

1717
Here is an example where this is useful:
1818

1919
```tut:fail
20-
trait Node[+T] {
21-
def prepend(elem: T)
20+
trait Node[+B] {
21+
def prepend(elem: B): Unit
2222
}
2323
24-
case class ListNode[+T](h: T, t: Node[T]) extends Node[T] {
25-
def prepend(elem: T) = ListNode[T](elem, this)
26-
def head: T = h
24+
case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {
25+
def prepend(elem: B) = ListNode[B](elem, this)
26+
def head: B = h
2727
def tail = t
2828
}
2929
30-
case class Nil[+T]() extends Node[T] {
31-
def prepend(elem: T) = ListNode[T](elem, this)
30+
case class Nil[+B]() extends Node[B] {
31+
def prepend(elem: B) = ListNode[B](elem, this)
3232
}
3333
```
34-
This program implements a singly-linked list. `Nil` represents an empty element (i.e. an empty list). `class ListNode` is a node which contains an element of type `T` (`head`) and a reference to the rest of the list (`tail`). The `class Node` and its subtypes are covariant because we have `+T`.
34+
This program implements a singly-linked list. `Nil` represents an empty element (i.e. an empty list). `class ListNode` is a node which contains an element of type `B` (`head`) and a reference to the rest of the list (`tail`). The `class Node` and its subtypes are covariant because we have `+B`.
3535

36-
However, this program does _not_ compile because the parameter `elem` in `prepend` is of type `T`, which we declared *co*variant. This doesn't work because functions are *contra*variant in their parameter types and *co*variant in their result types.
36+
However, this program does _not_ compile because the parameter `elem` in `prepend` is of type `B`, which we declared *co*variant. This doesn't work because functions are *contra*variant in their parameter types and *co*variant in their result types.
3737

38-
To fix this, we need to flip the variance of the type of the parameter `elem` in `prepend`. We do this by introducing a new type parameter `U` that has `T` as a lower type bound.
38+
To fix this, we need to flip the variance of the type of the parameter `elem` in `prepend`. We do this by introducing a new type parameter `U` that has `B` as a lower type bound.
3939

4040
```tut
41-
trait Node[+T] {
42-
def prepend[U >: T](elem: U)
41+
trait Node[+B] {
42+
def prepend[U >: B](elem: U)
4343
}
4444
45-
case class ListNode[+T](h: T, t: Node[T]) extends Node[T] {
46-
def prepend[U >: T](elem: U) = ListNode[U](elem, this)
47-
def head: T = h
45+
case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {
46+
def prepend[U >: B](elem: U) = ListNode[U](elem, this)
47+
def head: B = h
4848
def tail = t
4949
}
5050
51-
case class Nil[+T]() extends Node[T] {
52-
def prepend[U >: T](elem: U) = ListNode[U](elem, this)
51+
case class Nil[+B]() extends Node[B] {
52+
def prepend[U >: B](elem: U) = ListNode[U](elem, this)
5353
}
5454
```
5555

0 commit comments

Comments
 (0)