0

私の質問は次のとおりです。

  1. この警告は何ですか?Reflect.runtime.universe.TypeRef の型パラメータはありません
  2. asInstanceOfではなくメソッド呼び出しargでこれがスローされるのはなぜですか?

コンソール出力:

Welcome to Scala version 2.10.0 (Java HotSpot(TM) Client VM, Java 1.6.0_35).
Type in expressions to have them evaluated.
Type :help for more information.

scala>  import reflect.runtime.{universe => ru}
import reflect.runtime.{universe=>ru}

scala>  val t = ru.typeOf[Option[_]]
t: reflect.runtime.universe.Type = scala.Option[_]

scala>  t.isInstanceOf[ru.TypeRef]
<console>:10: warning: abstract type reflect.runtime.universe.TypeRef is unchecked since it is eliminated by erasure
              t.isInstanceOf[ru.TypeRef]
                            ^
res0: Boolean = true

scala>  t.asInstanceOf[ru.TypeRef]
res1: reflect.runtime.universe.TypeRef = scala.Option[_]

scala>  t.asInstanceOf[ru.TypeRef].args
java.lang.ClassCastException: scala.reflect.internal.Types$ExistentialType cannot be cast to scala.reflect.api.Types$TypeRefApi
    at .<init>(<console>:10)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:731)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:980)
    at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:570)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:601)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:565)
    at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702)
    at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566)
    at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:867)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822)
    at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)
    at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:889)
    at org.jetbrains.plugins.scala.compiler.rt.ConsoleRunner.main(ConsoleRunner.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:121)
4

1 に答える 1

3

TypeRefは抽象型であるため、上限まで消去されます。通常、これは、信頼できるパターン マッチングを行うことが不可能であることを意味しますisInstanceOf(最終的にはそれに依存します)。

ただし、リフレクション API は抽象型を広く使用するため、クラス タグに基づいて抽象型のインスタンスをパターン マッチする特別なメカニズムを組み込みました。スコープ内に抽象型に対応するクラス タグがある場合、すべてが確実に機能します。

当然のことながら、ほぼすべての抽象型ごとに 1 つずつ、ユニバースにバンドルされた一連のクラス タグを提供します。問題は、これらのクラス タグをインポートする必要があることです。ほとんどの人がそうしているので、通常は問題ありませんがimport ru._、そのような場合に発生します。これは問題として認識されていますが、まだ修正されていません。

于 2013-08-13T14:45:57.737 に答える