Skip to content

Override check is optional for contravariant traits #10079

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

Open
joroKr21 opened this issue Oct 24, 2020 · 0 comments
Open

Override check is optional for contravariant traits #10079

joroKr21 opened this issue Oct 24, 2020 · 0 comments
Labels
backlog No work planned on this by the core team for the time being. itype:bug

Comments

@joroKr21
Copy link
Member

joroKr21 commented Oct 24, 2020

Clone of scala/bug#12209 - I thought it's worth reporting here as well because it looks like a problematic interaction of language features: overriding + overloading + traits + variance.

Minimized code

object OverrideCheck extends App {
  trait A { def m(x: CharSequence): String = s"A: $x" }
  //trait B extends A { override def m(x: String): String = s"B: $x" } // no: m has a different signature

  trait C[-T <: CharSequence] { def m(x: T): String = s"C: $x" }
  //trait D extends C[String] { def m(x: String): String = "D" } // no: m needs override modifier
  trait D extends C[String] { override def m(x: String): String = s"D: $x" } // ok

  val x = new D with C[CharSequence] // ok: String <: CharSequence, C[CharSequence] <: C[String]
  println(x.m("x1")) // ok: D
  println((x: C[String]).m("x2")) // bad: C

  val y = new D {}
  println(y.m("y1")) // ok: D
  println((y: C[String]).m("y2")) // ok: D
}

I'm not sure if it's possible to observe this problem with a covariant trait.

Output

D: x1
C: x2
D: y1
D: y2

Expectation

I am not sure. I guess I shouldn't be able to define x.

I guess the problem is that we do the override check during method declaration, but the example shows that we can't know for sure at this point of time. Is it viable to do the override check for all members during class definition?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog No work planned on this by the core team for the time being. itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants