Skip to content

A ClassCastException is thrown due to incorrect LUB type #11684

@Atry

Description

@Atry
Welcome to Scala 2.13.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_202-ea).
Type in expressions for evaluation. Or try :help.

scala> object O {
     |     trait Base {
     |         private[O] type Output
     |         private[O] def output: Output
     |     }
     |     class IntOutput(val output: Int) extends Base {
     |         private[O] type Output = Int
     |     }
     |     class StringOutput1(private[O] val output: String) extends Base {
     |         private[O] type Output = String
     |     }
     |     class StringOutput2(private[O] val output: String) extends Base{
     |         private[O] type Output = String
     |     }
     |     def f: Base { type Output = String } => Int = _.output.length
     |     def f2: (StringOutput1 => Int) with (StringOutput2 => Int) = f
     | }
defined object O

scala> O.f2(new O.IntOutput(3))
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
  at O$.$anonfun$f$1(<console>:15)
  at O$.$anonfun$f$1$adapted(<console>:15)
  ... 28 elided

I think O.f2(new O.IntOutput(3)) should not compile, because IntOutput is neither StringOutput1 nor StringOutput2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions