Skip to content

Commit 741cd99

Browse files
authored
Add code tabs to type checker page (#2726)
1 parent 6fe9972 commit 741cd99

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Diff for: _overviews/scala3-migration/incompat-type-checker.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ It is unsound and might cause runtime failures, as demonstrated by this [test](h
1818

1919
The Scala 3 compiler does not permit this anymore.
2020

21-
```scala
21+
{% tabs scala-2-unsound_vc_1 %}
22+
{% tab 'Scala 2 Only' %}
23+
~~~ scala
2224
class Foo[-A](x: List[A]) {
2325
def f[B](y: List[B] = x): Unit = ???
2426
}
2527

2628
class Outer[+A](x: A) {
2729
class Inner(y: A)
2830
}
29-
```
31+
~~~
32+
{% endtab %}
33+
{% endtabs %}
3034

35+
So if you compile in Scala 3, you will get the following error.
3136
{% highlight text %}
3237
-- Error: src/main/scala/variance.scala:2:8
3338
2 | def f[B](y: List[B] = x): Unit = y
@@ -75,7 +80,9 @@ Scala 3 fixes some unsoundness bugs in pattern matching, preventing some semanti
7580

7681
For instance, the match expression in `combineReq` can be compiled with Scala 2.13 but not with Scala 3.
7782

78-
```scala
83+
{% tabs scala-2-unsound_pm_1 %}
84+
{% tab 'Scala 2 Only' %}
85+
~~~ scala
7986
trait Request
8087
case class Fetch[A](ids: Set[A]) extends Request
8188

@@ -88,9 +95,11 @@ object Request {
8895
}
8996
}
9097
}
91-
```
98+
~~~
99+
{% endtab %}
100+
{% endtabs %}
92101

93-
The error message is:
102+
In Scala 3, the error message is:
94103

95104
{% highlight text %}
96105
-- [E007] Type Mismatch Error: src/main/scala/pattern-match.scala:9:59
@@ -100,6 +109,7 @@ The error message is:
100109
| Required: Fetch[A$1]
101110
{% endhighlight %}
102111

112+
103113
Which is right, there is no proof that `x` and `y` have the same type parameter `A`.
104114

105115
Coming from Scala 2, this is clearly an improvement to help us locate mistakes in our code.
@@ -108,9 +118,13 @@ It is not always easy and sometimes it is even not possible, in which case the c
108118

109119
In this example, we can relax the constraint on `x` and `y` by stating that `A` is a common ancestor of both type arguments.
110120
This makes the compiler type-check the code successfully.
111-
112-
```scala
121+
{% tabs shared-unsound_pm_2 %}
122+
{% tab 'Scala 2 and 3' %}
123+
~~~ scala
113124
def combineFetch[A](x: Fetch[_ <: A], y: Fetch[_ <: A]): Fetch[A] = Fetch(x.ids ++ y.ids)
114-
```
125+
~~~
126+
{% endtab %}
127+
{% endtabs %}
115128

116129
Alternatively, a general but unsafe solution is to cast.
130+

0 commit comments

Comments
 (0)