[英]type mismatch error declaring list of classes
def getValueAndItsType() : List[ (AnyRef, Class[_]) ] = {
val dataSet1 = ("some string data", classOf[String])
val dataSet2 = (new Thread(), classOf[Thread])
val dataSet3 = (new NullPointerException(), classOf[NullPointerException])
val dataSet4 = (5, classOf[Int])
val list = List(dataSet1, dataSet2, dataSet3, dataSet4)
list
}
Type type mismatch;
类型类型不匹配; found : List[(Any, Class[_ >: Int with NullPointerException with Thread with String])] required: List[(AnyRef, Class[_])]
找到:列表[(Any,类[_>:具有NullPointerException的整数,带有带字符串的线程])]]必需:列表[(AnyRef,类[_])]
If dataSet4 is removed from List, the compile time error disappears
Please suggest, what is wrong with Class[_].
请提出,Class [_]有什么问题。 Isn't it equivalent to Class[?] in java ?
它不等于Java中的Class [?]吗? I appreciate, if you also suggest correct declaration for doing this..
如果您也建议为此声明正确,则不胜感激。
In Scala : 在Scala中 :
Any
is the root of the Scala class. Any
是Scala类的根。 AnyRef
is the root of the class of reference types , it extends from Any
. AnyRef
是引用类型类的根,它从Any
扩展。 AnyVal
is the root class of all value types . AnyVal
是所有值类型的根类。 it extends from Any
Any
延伸 Null
is a subtype of all reference types . Null
是所有引用类型的子类型 。 Nothing
is a subtype of all other types including Null
Nothing
是所有其他类型的子类型,包括Null
So base on your code, you need to extend from Any
, include AnyRef: reference types
and AnyVal: values types
. 因此,根据您的代码,您需要从
Any
进行扩展,包括AnyRef: reference types
和AnyVal: values types
。
def getValueAndItsType() : List[ (Any, _ <: Any) ] = {
val dataSet1 = ("some string data", classOf[String])
val dataSet2 = (new Thread(), classOf[Thread])
val dataSet3 = (new NullPointerException(), classOf[NullPointerException])
val list = List(dataSet1, dataSet2, dataSet3)
list
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.