The following program should not compile, but instead fails at runtime.
class Gox {
object Zed { }
class Zed { }
}
object Test {
type GoxZed = t.Zed forSome { val t: Gox }
def main(args: Array[String]): Unit = {
val x = new Gox
val y: GoxZed = x.Zed
}
}
% scala29 Test
java.lang.ClassCastException: Gox$$Zed$$ cannot be cast to Gox$$Zed
at Test$$.main(gox.scala:11)
at Test.main(gox.scala)
The type of y is declared to be an instance of the class Zed, but the compiler allows the object Zed to be assigned to it. We can verify this is a result of type/term blurring by making a small change:
class Gox {
object ZedZed { }
class Zed { }
}
object Test {
type GoxZed = t.Zed forSome { val t: Gox }
def main(args: Array[String]): Unit = {
val x = new Gox
val y: GoxZed = x.ZedZed
}
}
Now the compiler correctly fails it.
% scalac29 zed2.scala
zed2.scala:11: error: type mismatch;
found : x.ZedZed.type (with underlying type object x.ZedZed)
required: Test.GoxZed
val y: GoxZed = x.ZedZed
^
one error found
The following program should not compile, but instead fails at runtime.
The type of y is declared to be an instance of the class Zed, but the compiler allows the object Zed to be assigned to it. We can verify this is a result of type/term blurring by making a small change:
Now the compiler correctly fails it.