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
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at Baz$.foo(main.scala:7)
Expectation
The code should not compile. It should produce the following error:
object creation impossible, since def foo(x: A): Unit in trait Foo is not defined
(Note that
parameter A in def foo(x: A): Unit in trait Foo does not match
parameter A & String in def foo(x: A & String): Unit in trait Bar
type A & String is a subtype of trait A, but method parameter types must match exactly.)
Note: adding the override keyword raises a compile-time error:
traitFoo[A]:deffoo(x: A):UnittraitBar[A] extendsFoo[A]:overridedeffoo(x: A&String):Unit= println(x.toUpperCase)
// ^^^^^^^^^^// method foo has a different signature than the overridden declaration
The text was updated successfully, but these errors were encountered:
smarter
changed the title
Generic method parameter types can be refined covariantly
Overload that differs only in parameter type is mistaken as override, crashes with a ClassCastException at runtime
Jun 15, 2021
We now exclude a pair of overriding / overridden symbols that
have a common subclass parent of the base class only under the
additional condition that their signatures also match.
scala#12828 shows a case where this matters:
```scala
trait Foo[A]:
def foo(x: A): Unit
trait Bar[A] extends Foo[A]:
def foo(x: A & String): Unit = println(x.toUpperCase)
object Baz extends Bar[Int] // error overriding foo: incompatible type
@main def run() = Baz.foo(42)
```
When checking `Baz`, there is a common subclass parent (namely `Bar`),
but the signatures of the two `foo` definitions as seen from `Bar` are
different, so we cannot assume that the pair has already been checked.
Fixesscala#12828
We now exclude a pair of overriding / overridden symbols that
have a common subclass parent of the base class only under the
additional condition that their signatures also match.
scala#12828 shows a case where this matters:
```scala
trait Foo[A]:
def foo(x: A): Unit
trait Bar[A] extends Foo[A]:
def foo(x: A & String): Unit = println(x.toUpperCase)
object Baz extends Bar[Int] // error overriding foo: incompatible type
@main def run() = Baz.foo(42)
```
When checking `Baz`, there is a common subclass parent (namely `Bar`),
but the signatures of the two `foo` definitions as seen from `Bar` are
different, so we cannot assume that the pair has already been checked.
Fixesscala#12828
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.0.1-RC1
Minimized code
Output
The code compiles, but fails at run-time:
Expectation
The code should not compile. It should produce the following error:
Note: adding the
override
keyword raises a compile-time error:The text was updated successfully, but these errors were encountered: