Is this a type checking error?

I narrowed it down to the following code snippet:

trait A[T] {
  def apply(t: T): Int
}

sealed trait P {
  def apply(): Int
}

case class I[T](a: A[T], t: T) extends P {
  def apply: Int = a(t)
}

case class X[T1, T2](a1: A[T1], a2: A[T2]) extends A[(T1, T2)] {
  def apply(t: (T1, T2)): Int =
    t match {
      case (t1, t2) => a1(t1) + a2(t2)
    }
}

object m {
  def apply(p1: P, p2: P): P =
    (p1, p2) match {
      case (I(a1, t1), I(a2, t2)) =>
        I(X(a1, a2), (t2, t1)) // <-- Here
    }
}

As you can see, I have a type error in the line with the inscription <-- Here. And yet, the code compiles without warning, and ClassCastExceptiondoes not work at run time. Code to play:

case class E() extends A[Int] {
  def apply(t: Int): Int = t
}

case class S() extends A[String] {
  def apply(t: String): Int = t.length
}

object Test {
  def apply() = {
    val pe: P = I(E(), 3)
    val ps: P = I(S(), "abcd")
    val pp: P = m(pe, ps)
    pp()
  }
}

I know that when scala pattern matching sometimes fails to verify that the value is of the correct type, but this usually leads to a compiler warning.

So, is this a mistake, or am I missing something?

Update. My concern is that I can make a type error, and the compiler won't even warn me. I understand that (t1, t2)- the correct order; but if I write it incorrectly, I will not find it before the program runs, and possibly even later, although this is clearly a type error.

+4
2

, :

https://issues.scala-lang.org/browse/SI-9188

, A, , .

:

scala> val i = I(E(), 42)
i: I[Int] = I(E(),42)

scala> i match { case I(a: A[Int], x) => }

scala> i match { case I(a: A[String], x) => }
<console>:15: warning: non-variable type argument String in type pattern A[String] is unchecked since it is eliminated by erasure
              i match { case I(a: A[String], x) => }
                                  ^
<console>:15: error: pattern type is incompatible with expected type;
 found   : A[String]
 required: A[Int]
              i match { case I(a: A[String], x) => }
                                  ^

scala> (i: P) match { case I(a: A[String], x) => }
<console>:15: warning: non-variable type argument String in type pattern A[String] is unchecked since it is eliminated by erasure
              (i: P) match { case I(a: A[String], x) => }
                                       ^
<console>:15: error: pattern type is incompatible with expected type;
 found   : A[String]
 required: A[Any]
Note: String <: Any, but trait A is invariant in type T.
You may wish to define T as +T instead. (SLS 4.5)
              (i: P) match { case I(a: A[String], x) => }
                                       ^

scala> (i: P) match { case I(a: A[Int], x) => }
<console>:15: warning: non-variable type argument Int in type pattern A[Int] is unchecked since it is eliminated by erasure
              (i: P) match { case I(a: A[Int], x) => }
                                       ^
<console>:15: error: pattern type is incompatible with expected type;
 found   : A[Int]
 required: A[Any]
Note: Int <: Any, but trait A is invariant in type T.
You may wish to define T as +T instead. (SLS 4.5)
              (i: P) match { case I(a: A[Int], x) => }
                                       ^

scala> (i: P) match { case I(a: A[_], x) => }

scala> (i: P) match { case I(a: A[Any], x) => }

:

scala> (i: P) match { case I(a: A[Any], x) => a("foo") }
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
  at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:105)
  at E.apply(<console>:33)
  ... 33 elided
+4

: .

(p1, p2), , , , P, I[T], T. I[T], , P, , . ? . . , T , .

, m.apply I[T], T p1, p2.

object m {
  def apply[T](p1: I[T], p2: I[T]): P =
    (p1, p2) match {
      case (I(a1, t1), I(a2, t2)) =>
        I(X(a1, a2), (t2, t1))
  }
}

val pe = I(E(), 3)
val ps = I(S(), "abcd")

m(pe, pe).apply // same underlying type, works
m(ps, ps).apply // same underlying type, works
m(pe, ps).apply // doesn't compile

, , .

+1

All Articles