Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与scala反射库不一致_Scala_Generics_Reflection_Parameterized_Mirror - Fatal编程技术网

与scala反射库不一致

与scala反射库不一致,scala,generics,reflection,parameterized,mirror,Scala,Generics,Reflection,Parameterized,Mirror,我很难理解为什么在2.11.1中使用scala的运行时反射会产生看起来不一致的结果 我试图检查java对象中包含的字段类型,如下所示: import java.util.List; import java.util.ArrayList; public class Example { private List<Integer> listOfInts; public Example () { listOfInts = new ArrayList<Integer&

我很难理解为什么在2.11.1中使用scala的运行时反射会产生看起来不一致的结果

我试图检查java对象中包含的字段类型,如下所示:

import java.util.List;
import java.util.ArrayList;

public class Example {
  private List<Integer> listOfInts;

  public Example () {
    listOfInts = new ArrayList<Integer>();
  }  
}

但是,如果我在
之后重新分配字段类型,我不知道如何混合Java反射和Scala反射,但是符号确实需要初始化,因为我记得过去关于缺少它们的问题。当然,重新分配是不相关的,但可能在第一次
之后,反射库是基于编译器的,这是非常遗憾的。人们应该要求更好。不管怎样,事情就是这样

这是近两年前的一张样品票

在某处存在某种持久状态

几乎什么都没有


附录:要获得真正令人眩晕的体验,请浏览355张罚单。

Api是实验性的,请检查scala 2.11.2,然后创建问题
http://scala-lang.org/contribute/bug-reporting-guide.html
看起来像个bug,我很快会看一看。一定有人投了反对票,因为你没有用双关语来表达“进一步思考,…”。但罚单上的评论补充道,“调用toString有时会导致调用规范化(这是一个bug,我可能在master中修复了,记不起来),这会强制提供信息。”我当时笑着说(或者说,我猜)“我猜这是不应该发生的事情。”但后来我看到你已经把它拉出来了。好吧,刚刚注意到
JavaMirror扩展了funhousemrror
。伙计,你让我检查一下。
import java.lang.Class
import java.lang.reflect.Field
import java.util.List
import scala.reflect.runtime.{ universe => ru }

object Inspect extends scala.App {
  val example = new Example 
  val cls = example.getClass
  val listfield = cls.getDeclaredField("listOfInts")

  println(isListType(listfield)) // prints false 
  println(isListType(listfield)) // prints true, as do all subsequent calls

  def isListType (field: Field): Boolean = {
    /*
      A function that returns whether the type of the field is a list.
      Based on examples at http://docs.scala-lang.org/overviews/reflection/environment-universes-mirrors.html
    */
    val fieldcls = field.getType

    val mirror: ru.Mirror = ru.runtimeMirror(getClass.getClassLoader)
    val fieldsym: ru.ClassSymbol = mirror.classSymbol(fieldcls)
    val fieldtype: ru.Type = fieldsym.toType 

    (fieldtype <:< ru.typeOf[List[_]])
  }  
}
...
val example = new Example   
val cls = example.getClass
val listfield = cls.getDeclaredField("listOfInts")
val fieldcls = listfield.getType

val mirror: ru.Mirror = ru.runtimeMirror(getClass.getClassLoader)
val fieldsym: ru.ClassSymbol = mirror.classSymbol(fieldcls)
val fieldtype: ru.Type = fieldsym.toType 

println(fieldtype <:< ru.typeOf[List[_]]) // prints false
println(fieldtype <:< ru.typeOf[List[_]]) // prints false
// replace as under the fieldsym assignment
var fieldtype: ru.Type = fieldsym.toType 
println(fieldtype <:< ru.typeOf[List[_]]) // prints false

fieldtype = fieldsym.toType 
println(fieldtype <:< ru.typeOf[List[_]]) // prints true
// replace as under the fieldsym assignment
var fieldtype: ru.Type = fieldsym.toType 
fieldtype = fieldsym.toType 

println(fieldtype <:< ru.typeOf[List[_]]) // prints false
println(fieldtype <:< ru.typeOf[List[_]]) // prints false
scala> import reflect.runtime._ ; import universe._
import reflect.runtime._
import universe._

scala> typeOf[jex.Example]
res0: reflect.runtime.universe.Type = jex.Example

scala> .declarations
warning: there was one deprecation warning; re-run with -deprecation for details
res1: reflect.runtime.universe.MemberScope = SynchronizedOps(variable listOfInts, constructor Example)

scala> typeOf[jex.Example] member (TermName("listOfInts"))
res2: reflect.runtime.universe.Symbol = variable listOfInts

scala> .typeSignature
res3: reflect.runtime.universe.Type = java.util.List[Integer]
scala> import reflect.runtime._ ; import universe._
import reflect.runtime._
import universe._

scala> classOf[jex.Example].getDeclaredField("listOfInts").getType
res0: Class[_] = interface java.util.List

scala> currentMirror classSymbol res0 toType
warning: there was one feature warning; re-run with -feature for details
res1: reflect.runtime.universe.Type = java.util.List

scala> .<:<(typeOf[List[_]])
res2: Boolean = false

scala> currentMirror classSymbol res0 toType
warning: there was one feature warning; re-run with -feature for details
res3: reflect.runtime.universe.Type = java.util.List[E]

scala> .<:<(typeOf[List[_]])
res4: Boolean = false
scala> import reflect.runtime._ ; import universe._
import reflect.runtime._
import universe._

scala> val x = new jex.Example
x: jex.Example = jex.Example@1efed156

scala> x.getClass getDeclaredField "listOfInts" getType
warning: there was one feature warning; re-run with -feature for details
res0: Class[_] = interface java.util.List

scala> val m = runtimeMirror(getClass.getClassLoader)
m: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@1ffd0e4b of type class scala.tools.nsc.interpreter.IMain$TranslatingClassLoader with classpath [(memory)] and parent being scala.reflect.internal.util.ScalaClassLoader$URLClassLoader@3b084709 of type class scala.reflect.internal.util.ScalaClassLoader$URLClassLoader with classpath [file:/home/apm/jdk-8/jdk1.8.0_11/jre/lib/resources.jar,file:/home/apm/jdk-8/jdk1.8.0_11/jre/lib/rt.jar,file:/home/apm/jdk-8/jdk1.8.0_11/jre/lib/jsse.jar,file:/home/apm/jdk-8/jdk1.8.0_11/jre/lib/jce.jar,file:/home/apm/jdk-8/jdk1.8.0_11/jre/lib/charsets.jar,file:/home/apm/jdk-8/jdk1.8.0_11/jre/lib/jfr.jar,file:/home/apm/scala-2.11.2/lib/akka-actor_2.11-2.3.4.jar,file:/home/apm/scala-2.11.2/lib/config-1.2...
scala> val s = m classSymbol res0
s: reflect.runtime.universe.ClassSymbol = trait List

scala> var t = s.toType
t: reflect.runtime.universe.Type = java.util.List[E]

scala> t <:< typeOf[List[_]]
res1: Boolean = false

scala> t = s.toType
t: reflect.runtime.universe.Type = java.util.List[E]

scala> t <:< typeOf[List[_]]
res2: Boolean = false
val fieldsym: ru.ClassSymbol = mirror.classSymbol(fieldcls)
println(fieldsym)
val fieldtype: ru.Type = fieldsym.toType
println(fieldtype)
apm@mara:~/tmp$ vi jex/inspect.scala
apm@mara:~/tmp$ scalac jex/inspect.scala && scala jex.Inspect
false
true
apm@mara:~/tmp$ vi jex/inspect.scala
apm@mara:~/tmp$ scalac jex/inspect.scala && scala jex.Inspect
trait List
java.util.List[E]
true
trait List
java.util.List[E]
true