You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Scala 2.13 the return type of an override method can be inferred from the left hand side, whereas in Scala 3 it would be inherited from the base method.
4
+
5
+
```scala
6
+
classParent {
7
+
deffoo:Foo=newFoo
8
+
}
9
+
10
+
classChildextendsParent {
11
+
overridedeffoo=newRichFoo(super.foo)
12
+
}
13
+
```
14
+
15
+
In this example, `Child#foo` returns a `RichFoo` in its Scala 2.13 signature but a `Foo` in its Scala 3 signature.
16
+
It can lead to compiler errors as demonstrated below.
17
+
18
+
```scala
19
+
classFoo
20
+
classRichFoo(foo: Foo) extendsFoo {
21
+
defshow:String=""
22
+
}
23
+
24
+
classParent {
25
+
deffoo=newFoo
26
+
}
27
+
28
+
classChildextendsParent {
29
+
overridedeffoo=newRichFoo(super.foo)
30
+
}
31
+
32
+
objectTest {
33
+
(newChild).foo.show // Scala 3 error: value show is not a member of Foo
34
+
}
35
+
```
36
+
37
+
In some rare cases involving implicit conversions and runtime casting it could even cause an runtime failure.
38
+
39
+
The solution is to make the return type of the override method explicit:
40
+
41
+
```diff
42
+
class Child extends Parent {
43
+
- override def foo = new RichFoo(super.foo)
44
+
+ override def foo: RichFoo = new RichFoo(super.foo)
0 commit comments