How does Scala's type erasure work for higher kinded type parameters?

喜夏-厌秋 提交于 2019-12-24 00:26:41

问题


I don't understand which generic type parameters Scala erases. I used to think that it should erase all generic type parameters, but this does not seem to be the case.

Correct me if I'm wrong: if I instantiate an instance of type Map[Int, String] in the code, then at the runtime, the instance knows only that it is of type Map[_, _], and does not know anything about its generic type parameters. This is why the following succeeds to compile and to execute without errors:

val x: Map[Int, String] = Map(2 -> "a")
val y: Map[String, Int] = x.asInstanceOf[Map[String, Int]]

Now I would expect that all higher-kinded type parameters are also erased, that is, if I have a

class Foo[H[_, _], X, Y]

I would expect that an instance of type Foo[Map, Int, String] knows nothing about Map. But now consider the following sequence of type-cast "experiments":

import scala.language.higherKinds

def cast1[A](a: Any): A = a.asInstanceOf[A]
def cast2[H[_, _], A, B](a: Any) = a.asInstanceOf[H[A, B]]
def cast3[F[_[_, _], _, _], H[_, _], X, Y](a: Any) = a.asInstanceOf[F[H, X, Y]]
class CastTo[H[_, _], A, B] {
  def cast(x: Any): H[A, B] = x.asInstanceOf[H[A, B]]
}

ignoreException {
  val v1 = cast1[String](List[Int](1, 2, 3))                      
  // throws ClassCastException
}

ignoreException {
  val v2 = cast2[Map, Int, Long](Map[String, Double]("a" -> 1.0)) 
  // doesn't complain at all, `A` and `B` erased
}

ignoreException {
  // right kind
  val v3 = cast2[Map, Int, Long]((x: Int) => x.toLong)            
  // throws ClassCastException
}

ignoreException {
  // wrong kind
  val v4 = cast2[Map, Int, Long]("wrong kind")                     
  // throws ClassCastException
}

ignoreException {
  class Foo[H[_, _], X, Y](h: H[X, Y])
  val x = new Foo[Function, Int, String](n => "#" * n)
  val v5 = cast3[Foo, Map, Int, Long](x)
  // nothing happens, happily replaces `Function` by `Map`
}

ignoreException {
  val v6 = (new CastTo[Map, Int, Long]).cast(List("hello?"))       
  // throws ClassCastException
}

ignoreException {
  val castToMap = new CastTo[Map, Int, Long]
  val v7 = castToMap.cast("how can it detect this?")               
  // throws ClassCastException
}

ignoreException {
  val castToMap = new CastTo[Map, Int, Long]
  val madCast = castToMap.asInstanceOf[CastTo[Function, Float, Double]]
  val v8 = madCast.cast("what does it detect at all?")
  // String cannot be cast to Function???
  // Why does it retain any information about `Function` here?
}


// --------------------------------------------------------------------
var ignoreBlockCounter = 0
/** Executes piece of code, 
  * catches an exeption (if one is thrown),
  * prints number of `ignoreException`-wrapped block,
  * prints name of the exception.
  */
def ignoreException[U](f: => U): Unit = {
  ignoreBlockCounter += 1
  try {
    f
  } catch {
    case e: Exception =>
      println("[" + ignoreBlockCounter + "]" + e)
  }
}

Here is the output (scala -version 2.12.4):

[1]java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to java.lang.String
[3]java.lang.ClassCastException: Main$$anon$1$$Lambda$143/1744347043 cannot be cast to scala.collection.immutable.Map
[4]java.lang.ClassCastException: java.lang.String cannot be cast to scala.collection.immutable.Map
[6]java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to scala.collection.immutable.Map
[7]java.lang.ClassCastException: java.lang.String cannot be cast to scala.collection.immutable.Map
[8]java.lang.ClassCastException: java.lang.String cannot be cast to scala.Function1
  • The cases 1, 3, 4 indicate that asInstanceOf[Foo[...]] does care about Foo, this is expected.
  • The case 2 indicates that asInstanceOf[Foo[X,Y]] does not care about X and Y, this is also expected.
  • The case 5 indicates that asInstanceOf does not care about higher kinded type parameter Map, similar to case 2, this is also expected.

So far so good. However, the cases 6, 7, 8 suggest a different behavior: here, an instance of type CastTo[Foo, X, Y] seems to retain information about the generic type parameter Foo for some reason. More precisely, a CastTo[Map, Int, Long] seems to carry around enough information with it to know that a string cannot be cast into a Map. Moreover, in case 8, it seems to even change Map to Function because of a cast.

Question(s):

  1. Is my understanding correct that the first generic parameter of CastTo is not erased, or is there something else what I don't see? Some implicit operation or anything?
  2. Is there any documentation that describes this behavior?
  3. Is there any reason why I should want this behavior? I find it somewhat counter-intuitive, but maybe it's just me, and I'm using the tool wrong...

Thanks for reading.

EDIT: Poking around in similar examples revealed an issue with the 2.12.4-compiler (see my own "answer" below), but this is a separate issue.


回答1:


I think you are confusing some things.

Casts to generic types are deferred until the point where the types become concrete. For example, take this piece of code:

class CastTo[H[_, _], A, B] {
  def cast(x: Any): H[A, B] = x.asInstanceOf[H[A, B]]
}

In the bytecode you can only cast to a real class, because it doesn't know anything about generics. So the above will, in bytecode, be roughly equivalent to:

class CastTo {
  def cast(x: Object): Object = x
}

Then later in the code you give a String to method cast and the compiler can see that according to the type information it has, a Map[Int, Long] will come out. But in the bytecode cast has an erased return type of Object, so the compiler has to insert a cast at the use-site of the cast method. This code

val castToMap = new CastTo[Map, Int, Long]
val v7 = castToMap.cast("how can it detect this?")

will, in the bytecode, be roughly equivalent to the following (pseudo) code:

val castToMap = new CastTo
val v7 = castToMap.cast("how can it detect this?").asInstanceOf[Map]

As for your other questions:

  1. Not that I immediately know of.
  2. Why would you not want it? You are casting a String to a Map[Int, Long]. That is bound to crash eventually. Failing (relatively) fast with a ClassCastException is probably the safest, most user-friendly option.



回答2:


Ok, it's probably not the expected behavior.

Here is a randomized version of the above:

ignoreException {
  class Foo[X, Y]
  class Bar[X, Y]

  val castToMap = new CastTo[Map, Int, Long]
  val madderCast = if (math.random() > 0.5) {
    println("Foo")
    castToMap.asInstanceOf[CastTo[Foo, Float, Double]]
  } else {
    println("Bar")
    castToMap.asInstanceOf[CastTo[Bar, String, Any]]
  }
  val v9 = madderCast.cast("crash 'dis serva, awww yeah!")
  // crashes the compile server for scala 2.12.4
  // dotty 0.4.0-RC1 doesn't complain at all (as expected!)
}

crashes the 2.12.4 compiler. Dotty has no problems.

EDIT Shorter, self-contained example:

import scala.language.higherKinds

class Foo[X]
class Bar[X]
class CrashIt[A[_]] {
  def cast(a: Any): A[Int] = a.asInstanceOf[A[Int]]
}
val c = if (true) new CrashIt[Foo] else new CrashIt[Bar]
val x = c.cast("")

Crashes with:

java.util.NoSuchElementException: head of empty list
    at scala.collection.immutable.Nil$.head(List.scala:428)
    at scala.collection.immutable.Nil$.head(List.scala:425)
    at scala.tools.nsc.typechecker.ContextErrors$InferencerContextErrors$InferErrorGen$.
      NotWithinBoundsErrorMessage(ContextErrors.scala:1045)
    at scala.tools.nsc.typechecker.ContextErrors$InferencerContextErrors$InferErrorGen$.
      NotWithinBoundsContextErrors.scala:1052)
    at scala.tools.nsc.typechecker.Infer$Inferencer.issueBoundsError$1(Infer.scala:881)
    at scala.tools.nsc.typechecker.Infer$Inferencer.check$1(Infer.scala:887)
    at scala.tools.nsc.typechecker.Infer$Inferencer.checkBounds(Infer.scala:891)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.checkBounds(RefChecks.scala:1203)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.checkTypeRef(RefChecks.scala:1384)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.$anonfun$transform$4(RefChecks.scala:1668)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.$anonfun$transform$4$adapted(RefChecks.scala:1659)
    at scala.reflect.internal.tpe.TypeMaps$ForEachTypeTraverser.traverse(TypeMaps.scala:1102)
    at scala.reflect.internal.Types$Type.foreach(Types.scala:787)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1407)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:107)
    at scala.reflect.internal.Trees.$anonfun$itransform$1(Trees.scala:1369)
    at scala.reflect.api.Trees$Transformer.atOwner(Trees.scala:2600)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1368)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStat(RefChecks.scala:1186)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.$anonfun$transformStats$1(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:107)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1416)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:107)
    at scala.reflect.api.Trees$Transformer.transformTemplate(Trees.scala:2563)
    at scala.reflect.internal.Trees.$anonfun$itransform$4(Trees.scala:1420)
    at scala.reflect.api.Trees$Transformer.atOwner(Trees.scala:2600)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1419)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStat(RefChecks.scala:1198)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.$anonfun$transformStats$1(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:107)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1378)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:107)
    at scala.reflect.internal.Trees.$anonfun$itransform$2(Trees.scala:1375)
    at scala.reflect.api.Trees$Transformer.atOwner(Trees.scala:2600)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1373)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStat(RefChecks.scala:1198)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.$anonfun$transformStats$1(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:107)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1416)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:107)
    at scala.reflect.api.Trees$Transformer.transformTemplate(Trees.scala:2563)
    at scala.reflect.internal.Trees.$anonfun$itransform$5(Trees.scala:1425)
    at scala.reflect.api.Trees$Transformer.atOwner(Trees.scala:2600)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1424)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStat(RefChecks.scala:1198)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.$anonfun$transformStats$1(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:1169)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transformStats(RefChecks.scala:107)
    at scala.reflect.internal.Trees.$anonfun$itransform$7(Trees.scala:1438)
    at scala.reflect.api.Trees$Transformer.atOwner(Trees.scala:2600)
    at scala.reflect.internal.Trees.itransform(Trees.scala:1438)
    at scala.reflect.internal.Trees.itransform$(Trees.scala:1348)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itransform(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Transformer.transform(Trees.scala:2555)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:1751)
    at scala.tools.nsc.typechecker.RefChecks$RefCheckTransformer.transform(RefChecks.scala:107)
    at scala.tools.nsc.ast.Trees$Transformer.transformUnit(Trees.scala:140)
    at scala.tools.nsc.transform.Transform$Phase.apply(Transform.scala:30)
    at scala.tools.nsc.Global$GlobalPhase.$anonfun$applyPhase$1(Global.scala:436)
    at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:429)
    at scala.tools.nsc.Global$GlobalPhase.$anonfun$run$1(Global.scala:400)
    at scala.tools.nsc.Global$GlobalPhase.$anonfun$run$1$adapted(Global.scala:400)
    at scala.collection.Iterator.foreach(Iterator.scala:929)
    at scala.collection.Iterator.foreach$(Iterator.scala:929)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
    at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:400)
    at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1452)
    at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1436)
    at scala.tools.nsc.Global$Run.compileSources(Global.scala:1429)
    at scala.tools.nsc.Global$Run.compile(Global.scala:1545)
    at scala.tools.nsc.StandardCompileServer.session(CompileServer.scala:150)
    at scala.tools.util.SocketServer.$anonfun$doSession$2(SocketServer.scala:80)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
    at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
    at scala.Console$.withOut(Console.scala:163)
    at scala.tools.util.SocketServer.$anonfun$doSession$1(SocketServer.scala:80)
    at scala.tools.util.SocketServer.$anonfun$doSession$1$adapted(SocketServer.scala:75)
    at scala.tools.nsc.io.Socket.applyReaderAndWriter(Socket.scala:49)
    at scala.tools.util.SocketServer.doSession(SocketServer.scala:75)
    at scala.tools.util.SocketServer.loop$1(SocketServer.scala:91)
    at scala.tools.util.SocketServer.run(SocketServer.scala:103)
    at scala.tools.nsc.CompileServer$.$anonfun$execute$3(CompileServer.scala:216)
    at scala.runtime.java8.JFunction0$mcZ$sp.apply(JFunction0$mcZ$sp.java:12)
    at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
    at scala.Console$.withOut(Console.scala:163)
    at scala.tools.nsc.CompileServer$.$anonfun$execute$2(CompileServer.scala:211)
    at scala.runtime.java8.JFunction0$mcZ$sp.apply(JFunction0$mcZ$sp.java:12)
    at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
    at scala.Console$.withErr(Console.scala:192)
    at scala.tools.nsc.CompileServer$.main(CompileServer.scala:211)
    at scala.tools.nsc.CompileServer.main(CompileServer.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at scala.reflect.internal.util.ScalaClassLoader.$anonfun$run$2(ScalaClassLoader.scala:99)
    at scala.reflect.internal.util.ScalaClassLoader.asContext(ScalaClassLoader.scala:34)
    at scala.reflect.internal.util.ScalaClassLoader.asContext$(ScalaClassLoader.scala:30)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:125)
    at scala.reflect.internal.util.ScalaClassLoader.run(ScalaClassLoader.scala:99)
    at scala.reflect.internal.util.ScalaClassLoader.run$(ScalaClassLoader.scala:91)
    at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:125)
    at scala.tools.nsc.CommonRunner.run(ObjectRunner.scala:22)
    at scala.tools.nsc.CommonRunner.run$(ObjectRunner.scala:21)
    at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
    at scala.tools.nsc.CommonRunner.runAndCatch(ObjectRunner.scala:29)
    at scala.tools.nsc.CommonRunner.runAndCatch$(ObjectRunner.scala:28)
    at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:66)
    at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:85)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)    

The casting step seems to be crucial. If the invocation of cast is removed, nothing happens.

This does not happen if the parameter A is not higher-kinded, so for example for class CrashIt[A] and new CrashIt[Int] the compiler does not complain.

EDIT-2: Couldn't find this exact bug (or an obvious generalization thereof), reported it here: https://github.com/scala/bug/issues/10700 .



来源:https://stackoverflow.com/questions/48434656/how-does-scalas-type-erasure-work-for-higher-kinded-type-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!