如何在 Scala 中查找值类型的实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/41649294/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find instance of a value type in Scala?
提问by Bala
I know isInstanceOfcan be used to find value type, but how do I find the type of 'str'?  
我知道isInstanceOf可用于查找值类型,但是如何查找“str”的类型?  
What type is it?
它是什么类型?
scala> val str = ("Scala", "Elixir","Spark")
str: (String, String, String) = (Scala, Elixir, Spark)
The following throws an error (exclude Any/AnyRef etc for now):
以下会引发错误(暂时排除 Any/AnyRef 等):
scala> str.isInstanceOf[List]
<console>:13: error: type List takes type parameters
       str.isInstanceOf[List]
scala> str.isInstanceOf[String]
<console>:13: warning: fruitless type test: a value of type (String, String, String) cannot also be a String (the underlying of String)
       str.isInstanceOf[String]
                       ^
res9: Boolean = false
I can check it this way but is there a name for this?
我可以通过这种方式检查它,但是有名称吗?
scala> str.isInstanceOf[(String, String, String)]
res12: Boolean = true
回答by pamu
Use :typein scala repl to find type
:type在 scala repl 中使用以查找类型
Actual type is Tuple3[String, String, String]
实际类型是 Tuple3[String, String, String]
str.isInstanceOf[Tuple3[String, String, String]]
Scala REPL
Scala REPL
scala> val str = ("Scala", "Elixir","Spark")
str: (String, String, String) = (Scala,Elixir,Spark)
scala> :type str
(String, String, String)
scala> str.isInstanceOf[Tuple3[String, String, String]]
res2: Boolean = true
scala> str.getClass
res3: Class[_ <: (String, String, String)] = class scala.Tuple3
回答by fabfas
How to determine val type programmatically?
如何以编程方式确定 val 类型?
For instance, you want to know the type of the value is some specific type?
例如,您想知道值的类型是某种特定类型吗?
// assign x value
val x: Any = "this is string"
// custom function evaluate type
def f[T](v: T) = v match {
  case _: Int    => println("Int")
  case _: String => println("String")
  case _         => println("Unknown")
}
// determine val type
f(x)
回答by Alexey Svyatkovskiy
Yet another way to determine a type programmatically is using Manifest:
另一种以编程方式确定类型的方法是使用 Manifest:
scala> def getType[T: Manifest](t: T): Manifest[T] = manifest[T]
getType: [T](t: T)(implicit evidence: Manifest[T])Manifest[T]
scala> val str = ("Scala", "Elixir","Spark")
str: (String, String, String) = (Scala,Elixir,Spark)
scala> getType(str)
res0: Manifest[(String, String, String)] = scala.Tuple3[java.lang.String, java.lang.String, java.lang.String]
回答by Joseph
This method can help you get the type of any val/var at runtime, it's also works in compiled code.
此方法可以帮助您在运行时获取任何 val/var 的类型,它也适用于已编译的代码。
import scala.reflect.runtime.universe._
def printType[T](x: T)(implicit tag: TypeTag[T]): Unit = println(tag.tpe.toString)
printType(List[Int](1,2,3))  // output: List[Int]
printType(("xxx", 123, 0.1)) // output: (String, Int, Double)
printType(2)                 // output: Int
回答by Jasper-M
The modern (meaning Scala 2.10 or later), Scala way of programmatically getting the compile time type of something is using a TypeTag. 
现代(意味着 Scala 2.10 或更高版本),Scala 以编程方式获取某事物的编译时类型的方法是使用TypeTag.
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> def getType[T: TypeTag](value: T) = typeOf[T]
getType: [T](value: T)(implicit evidence: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.Type
scala> val str = ("Scala", "Elixir","Spark")
str: (String, String, String) = (Scala,Elixir,Spark)
scala> println(getType(str))
(java.lang.String, java.lang.String, java.lang.String)
scala> getType(str) <:< typeOf[(String,String,String)]
res1: Boolean = true
scala> getType((1,2,3)) <:< typeOf[(String,String,String)]
res2: Boolean = false
getClasswill give your the erased runtime class. isInstanceOf[T]will test whether the erased runtime class is the same as or a subclass of the erased runtime class of T.
getClass会给你擦除的运行时类。isInstanceOf[T]将测试擦除的运行时类是否与 的擦除运行时类相同或子类T。
And "erased" means that the following evaluates to true.
“擦除”意味着以下评估为true.
(1,2,3).isInstanceOf[(String,String,String)]
"runtime" and "compile time" mean that this is true:
“运行时”和“编译时”的意思是true:
val a: Any = (1,2,3)
a.isInstanceOf[(_,_,_)]
while this is false:
虽然这是false:
val a: Any = (1,2,3)
getType(a) <:< typeOf[(Int,Int,Int)]

