0

This example code does not behave as expected.

trait A

trait C

class X() {
  object B {
    def apply(): A = new B{}
  }
  object BC {
    def apply(): A = new B with C {}
  }
  class B extends A

}

val x1 = new X()
val x2 = new X()

val x1b = x1.B()

//this works
x1b match {
  case x2b: x2.B => "X2.B"
  case x1b: x1.B => "X1.B"
} //res: X1.B

//this does not test if B is instance from x2???
if(x1b.isInstanceOf[x2.B]) "X2.B" else if(x1b.isInstanceOf[x1.B]) "X1.B" else "" //res: X2.B

val x1bc = x1.BC()

//when matching for compounded types inner class origin is ignored???
x1bc match {
  case x2bc: x2.B with C => "X2.BC"
  case x1bc: x1.B with C => "X1.BC"
}//res: X2.BC
//does not work
if(x1bc.isInstanceOf[x2.B with C]) "X2.BC" else if(x1bc.isInstanceOf[x1.B with C]) "X1.BC" else "" //res: X2.BC

Why is pattern matching ignoring inner class types for compounded types?

Why is instanceOf ignoring inner class types at all?

tested Scala 2.11 and 2.12

scastie-example: https://scastie.scala-lang.org/ugepOPOtRcu6IenIaw3kAQ

user3508638
  • 195
  • 1
  • 7
  • Please, look at Path dependent types - https://stackoverflow.com/questions/2693067/what-is-meant-by-scalas-path-dependent-types – Ra Ka Dec 10 '18 at 11:28
  • Pattern-matching looks like a bug to me, according to https://www.scala-lang.org/files/archive/spec/2.12/08-pattern-matching.html#type-patterns. – Alexey Romanov Dec 10 '18 at 12:10

0 Answers0