Closed
Description
dotty considers that p
of type => P
is stable, so that p.T
is a valid type, but that's unsound:
object test {
trait Consumer {
type T
def eat(t: T): Unit
val defaultValue: T
}
class IntConsumer extends Consumer {
type T = Int
def eat(i: Int): Unit = println(i*i)
val defaultValue = 0
}
class StringConsumer extends Consumer {
type T = String
def eat(s: String): Unit = println(s.length)
val defaultValue = ""
}
var counter: Int = 0
def unstableConsumer: Consumer = {
counter += 1
if (counter % 2 == 0) new IntConsumer else new StringConsumer
}
def feed(c: => Consumer): Unit = {
val v: c.T = c.defaultValue // v: String = ""
c.eat(v) // c: IntConsumer, will crash
}
feed(unstableConsumer)
}
Scala 2 rejects it:
error: stable identifier required, but c found.
val v: c.T = c.defaultValue // v: String = ""
^
but dotty accepts it.