Skip to content

Add code tabs for _tour/lower-type-bounds #2549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions _tour/lower-type-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ While [upper type bounds](upper-type-bounds.html) limit a type to a subtype of a

Here is an example where this is useful:

{% tabs upper-type-bounds_1 class=tabs-scala-version %}
{% tab 'Scala 2' for=upper-type-bounds_1 %}
```scala mdoc:fail
trait List[+A] {
def prepend(elem: A): NonEmptyList[A] = NonEmptyList(elem, this)
Expand All @@ -24,13 +26,27 @@ case class NonEmptyList[+A](head: A, tail: List[A]) extends List[A]

object Nil extends List[Nothing]
```
{% endtab %}
{% tab 'Scala 3' for=upper-type-bounds_1 %}
```scala
trait List[+A]:
def prepend(elem: A): NonEmptyList[A] = NonEmptyList(elem, this)

case class NonEmptyList[+A](head: A, tail: List[A]) extends List[A]

object Nil extends List[Nothing]
```
{% endtab %}
{% endtabs %}

This program implements a singly-linked list. `Nil` represents an empty list with no elements. `class NonEmptyList` is a node which contains an element of type `A` (`head`) and a reference to the rest of the list (`tail`). The `trait List` and its subtypes are covariant because we have `+A`.

However, this program does _not_ compile because the parameter `elem` in `prepend` is of type `A`, 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.

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 `B` that has `A` as a lower type bound.

{% tabs upper-type-bounds_2 class=tabs-scala-version %}
{% tab 'Scala 2' for=upper-type-bounds_2 %}
```scala mdoc
trait List[+A] {
def prepend[B >: A](elem: B): NonEmptyList[B] = NonEmptyList(elem, this)
Expand All @@ -40,7 +56,23 @@ case class NonEmptyList[+A](head: A, tail: List[A]) extends List[A]

object Nil extends List[Nothing]
```
{% endtab %}
{% tab 'Scala 3' for=upper-type-bounds_2 %}
```scala
trait List[+A]:
def prepend[B >: A](elem: B): NonEmptyList[B] = NonEmptyList(elem, this)

case class NonEmptyList[+A](head: A, tail: List[A]) extends List[A]

object Nil extends List[Nothing]
```
{% endtab %}
{% endtabs %}

Now we can do the following:

{% tabs upper-type-bounds_3 %}
{% tab 'Scala 2 and 3' for=upper-type-bounds_3 %}
```scala mdoc
trait Bird
case class AfricanSwallow() extends Bird
Expand All @@ -65,6 +97,9 @@ val allBirds = africanSwallows.prepend(EuropeanSwallow())
// but this is a mistake! adding a list of birds widens the type arg too much. -Xlint will warn!
val error = moreBirds.prepend(swallowsFromAntarctica) // List[Object]
```
{% endtab %}
{% endtabs %}

The covariant type parameter allows `birds` to get the value of `africanSwallows`.

The type bound on the type parameter for `prepend` allows adding different varieties of swallows and getting a wider type: instead of `List[AfricanSwallow]`, we get a `List[Bird]`.
Expand Down