简体   繁体   中英

Scala: Typecast without explicitly known type parameter

Consider the following example:

case class C[T](x:T) {
  def f(t:T) = println(t)
  type ValueType = T
}

val list = List(1 -> C(2), "hello" -> C("goodbye"))

for ((a,b) <- list) {
  b.f(a)
}

In this example, I know (runtime guarantee) that the type of a will be some T , and b will have type C[T] with the same T . Of course, the compiler cannot know that, hence we get a typing error in bf(a) .

To tell the compiler that this invocation is OK, we need to do a typecast à la bf(a.asInstanceOf[T]) . Unfortunately, T is not known here. So my question is: How do I rewrite bf(a) in order to make this code compile?

I am looking for a solution that does not involve complex constructions (to keep the code readable), and that is "clean" in the sense that we should not rely on code erasure to make it work (see the first approach below).

I have some working approaches, but I find them unsatisfactory for various reasons.

Approaches I tried:

b.asInstanceOf[C[Any]].f(a)

This works, and is reasonably readable, but it is based on a "lie". b is not of type C[Any] , and the only reason we do not get a runtime error is because we rely on the limitations of the JVM (type erasure). I think it is good style only to use x.asInstanceOf[X] when we know that x is really of type X .

  b.f(a.asInstanceOf[b.ValueType])

This should work according to my understanding of the type system. I have added the member ValueType to the class C in order to be able to explicitly refer to the type parameter T . However, in this approach we get a mysterious error message:

Error:(9, 22) type mismatch;
 found   : b.ValueType
    (which expands to)  _1
 required: _1
  b.f(a.asInstanceOf[b.ValueType])
                    ^

Why? It seems to complain that we expect type _1 but got type _1 ! (But even if this approach works, it is limited to the cases where we have the possibility to add a member ValueType to C . If C is some existing library class, we cannot do that either.)

for ((a,b) <- list.asInstanceOf[List[(T,C[T]) forSome {type T}]]) {
  b.f(a)
}

This one works, and is semantically correct (ie, we do not "lie" when invoking asInstanceOf ). The limitation is that this is somewhat unreadable. Also, it is somewhat specific to the present situation: if a,b do not come from the same iterator, then where can we apply this type cast? (This code also has the side effect of being too complex for Intelli/J IDEA 2016.2 which highlights it as an error in the editor.)

  val (a2,b2) = (a,b).asInstanceOf[(T,C[T]) forSome {type T}]
  b2.f(a2)

I would have expected this one to work since a2,b2 now should have types T and C[T] for the same existential T . But we get a compile error:

Error:(10, 9) type mismatch;
 found   : a2.type (with underlying type Any)
 required: T
  b2.f(a2)
       ^

Why? (Besides that, the approach has the disadvantage of incurring runtime costs (I think) because of the creation and destruction of a pair.)

  b match {
    case b : C[t] => b.f(a.asInstanceOf[t])
  }

This works. But enclosing the code with a match makes the code much less readable. (And it also is too complicated for Intelli/J.)

The cleanest solution is, IMO, the one you found with the type-capture pattern match. You can make it concise, and hopefully readable, by integrating the pattern directly inside your for comprehension, as follows:

for ((a, b: C[t]) <- list) {
  b.f(a.asInstanceOf[t])
}

Fiddle: http://www.scala-js-fiddle.com/gist/b9030033133ee94e8c18ad772f3461a0

If you are not in a for comprehension already, unfortunately the corresponding pattern assignment does not work:

val (c, d: C[t]) = (a, b)
d.f(c.asInstanceOf[t])

That's because t is not in scope anymore on the second line. In that case, you would have to use the full pattern matching.

Maybe I'm confused about what you are trying to achieve, but this compiles:

case class C[T](x:T) {
  def f(t:T) = println(t)
  type ValueType = T
}

type CP[T] = (T, C[T])
val list = List[CP[T forSome {type T}]](1 -> C(2), "hello" -> C("goodbye"))

for ((a,b) <- list) {
  b.f(a)
}

Edit

If the type of the list itself is out of your control, you can still cast it to this "correct" type.

case class C[T](x:T) {
  def f(t:T) = println(t)
  type ValueType = T
}

val list = List(1 -> C(2), "hello" -> C("goodbye"))

type CP[T] = (T, C[T])
for ((a,b) <- list.asInstanceOf[List[CP[T forSome { type T }]]]) {
  b.f(a)
}

Great question! Lots to learn here about Scala.

Other answers and comments have already addressed most of the issues here, but I'd like to address a few additional points.

You asked why this variant doesn't work:

val (a2,b2) = (a,b).asInstanceOf[(T,C[T]) forSome {type T}]
b2.f(a2)

You aren't the only person who's been surprised by this; see eg this recent very similar issue report: SI-9899 .

As I wrote there:

I think this is working as designed as per SLS 6.1 : "The following skolemization rule is applied universally for every expression: If the type of an expression would be an existential type T, then the type of the expression is assumed instead to be a skolemization of T."

Basically, every time you write a value-level expression that the compiler determines to have an existential type, the existential type is instantiated. b2.f(a2) has two subexpressions with existential type, namely b2 and a2 , so the existential gets two different instantiations.

As for why the pattern-matching variant works, there isn't explicit language in SLS 8 (Pattern Matching) covering the behavior of existential types, but 6.1 doesn't apply because a pattern isn't technically an expression, it's a pattern. The pattern is analyzed as a whole and any existential types inside only get instantiated (skolemized) once.

As a postscript, note that yes, when you play in this area, the error messages you get are often confusing or misleading and ought to be improved. See for example https://github.com/scala/scala-dev/issues/205

A wild guess, but is it possible that you need something like this:

case class C[+T](x:T) {
  def f[A >: T](t: A) = println(t)
}

val list = List(1 -> C(2), "hello" -> C("goodbye"))

for ((a,b) <- list) {
  b.f(a)
}

?

It will type check.

I'm not quite sure what "runtime guarantee" means here, usually it means that you are trying to fool type system (eg with asInstanceOf ), but then all bets are off and you shouldn't expect type system to be of any help.

UPDATE

Just for the illustration why type casting is an evil:

case class C[T <: Int](x:T) {
  def f(t: T) = println(t + 1)
}

val list = List("hello" -> C(2), 2 -> C(3))

for ((a, b: C[t]) <- list) {
  b.f(a.asInstanceOf[t])
}

It compiles and fails at runtime (not surprisingly).

UPDATE2

Here's what generated code looks like for the last snippet (with C[t] ):

...
val a: Object = x1._1();
val b: Test$C = x1._2().$asInstanceOf[Test$C]();
if (b.ne(null))
  {
    <synthetic> val x2: Test$C = b;
    matchEnd4({
      x2.f(scala.Int.unbox(a));
      scala.runtime.BoxedUnit.UNIT
    })
  }
...

Type t simply vanished (as it should have been) and Scala is trying to convert a to an upper bound of T in C , ie Int . If there is no upper bound it's going to be Any (but then method f is nearly useless unless you cast again or use something like println which takes Any ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM