![](/img/trans.png)
[英]Benefit of explicitly providing the method return type or variable type in scala
[英]return type of the variable in Scala
如何在Scala中查看变量的类型?
我试图这样做:
val x = 10
println(type(x))
要么
val x = 'Hello!'
println(x.type)
不幸的是,这两种方式的包子我都出错了。
根据您要执行的操作,这可能就足够了
val x=10
println(x.getClass.toString)
但是,由于类型擦除,这种情况会恶化; Scala比Java具有更多的信息,以上内容仅提供Java的视图。 这里有一个主题更多的主题。 结果是:
def manOf[T:Manifest](t:T):Manifest[T] = manifest[T]
println(manOf(1))
println(manOf(List(1,2,3)))
让你
Int
scala.collection.immutable.List[Int]
.getClass.toString
方法只会为您提供一个int
和一个神秘的class scala.collection.immutable.$colon$colon
当然,如果您使用的是REPL Shell,它仍然会告诉您(标量)类型的事物:
$ scala
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val x=10
x: Int = 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.