在 Scala 中在运行时获取类型的字符串表示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/190368/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:18:05  来源:igfitidea点击:

Getting the string representation of a type at runtime in Scala

scalatypes

提问by Kevin Albrecht

In Scala, is it possible to get the string representation of a type at runtime? I am trying to do something along these lines:

在 Scala 中,是否可以在运行时获取类型的字符串表示?我正在尝试按照以下方式做一些事情:

def printTheNameOfThisType[T]() = {
  println(T.toString)
}

采纳答案by svrist

Note: this answer is out of date!

注意:此答案已过时!

Please see answer using TypeTag for Scala 2.10 and above

请参阅使用 TypeTag for Scala 2.10 及更高版本的答案

May I recommend #Scala on freenode

我可以在freenode上推荐#Scala吗

10:48 <seet_> http://stackoverflow.com/questions/190368/getting-the-string-representation-of-a-type-at-runtime-in-scala <-- isnt this posible?
10:48 <seet_> possible
10:48 <lambdabot> Title: Getting the string representation of a type at runtime in Scala - Stack Overflow,
                  http://tinyurl.com/53242l
10:49 <mapreduce> Types aren't objects.
10:49 <mapreduce> or values
10:49 <mapreduce> println(classOf[T]) should give you something, but probably not what you want.

Description of classOf

classOf 的描述

回答by Julie

In Scala 2.10 and above, use TypeTag, which contains full type information. You'll need to include the scala-reflectlibrary in order to do this:

在 Scala 2.10 及更高版本中,使用TypeTag包含完整类型信息的 。您需要包含scala-reflect库才能执行此操作:

import scala.reflect.runtime.universe._
def printTheNameOfThisType[T: TypeTag]() = {
  println(typeOf[T].toString)
}

You will get results like the following:

你会得到如下结果:

scala> printTheNameOfThisType[Int]
Int

scala> printTheNameOfThisType[String]
String

scala> printTheNameOfThisType[List[Int]]
scala.List[Int]

回答by Julie

There's a new, mostly-undocumented feature called "manifests" in Scala; it works like this:

Scala 中有一个新的、几乎没有记录的特性,称为“清单”;它是这样工作的:

object Foo {
  def apply[T <: AnyRef](t: T)(implicit m: scala.reflect.Manifest[T]) = println("t was " + t.toString + " of class " + t.getClass.getName() + ", erased from " + m.erasure)
}

The AnyRef bound is just there to ensure the value has a .toString method.

AnyRef 边界只是为了确保该值具有 .toString 方法。

回答by svrist

Please note that this isn't really "the thing:"

请注意,这并不是真正的“事情”:

object Test {
    def main (args : Array[String]) {
    println(classOf[List[String]])
    }
}

gives

$ scala Test                    
class scala.List

I think you can blame this on erasure

我想你可以把这归咎于擦除

====EDIT==== I've tried doing it with a method with a generic type parameter:

====编辑==== 我试过用一个带有泛型类型参数的方法来做到这一点:

object TestSv {
  def main(args:Array[String]){
    narf[String]
  }
  def narf[T](){
    println(classOf[T])
  }
}

And the compiler wont accept it. Types arn't classes is the explanation

编译器不会接受它。类型不是类是解释