Inferred type in a Scala program

2020-06-29 21:46发布

The Scala REPL shows the inferred type of an expression. Is there a way to know the inferred type in a normal Scala program ?

For example,

val x = {
//some Scala expressions
}

Now I want to know the actual type of x.

2条回答
地球回转人心会变
2楼-- · 2020-06-29 22:23

The type of the expression is known statically at compile time.

To access it at runtime, you could use a TypeTag as in the other answer, or a trivial macro:

scala> import scala.language.experimental.macros
import scala.language.experimental.macros

scala> import reflect.macros.blackbox.Context
import reflect.macros.blackbox.Context

scala> def impl(c: Context)(x: c.Expr[Any]): c.Expr[String] = { import c.universe._
     | c.Expr[String](Literal(Constant(c.typecheck(x.tree.duplicate).tpe.toString))) }
impl: (c: scala.reflect.macros.blackbox.Context)(x: c.Expr[Any])c.Expr[String]

scala> def f(x: =>Any) = macro impl
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
defined term macro f: (x: => Any)String

scala> trait A; trait B extends A; trait C extends A
defined trait A
defined trait B
defined trait C

scala> f(List(new B{}, new C{}))
res2: String = List[A]

The REPL also just reports the type assigned to an expression tree by the compiler.

查看更多
够拽才男人
3楼-- · 2020-06-29 22:24

Perhaps TypeTag is what you are looking for?

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> def typeOf[T](x:T)( implicit tag: TypeTag[T] ) = tag
typeOf: [T](x: T)(implicit tag: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.TypeTag[T]

scala> class Foo( a:Int )
defined class Foo

scala> trait Bar
defined trait Bar

scala> val x = new Foo(3) with Bar
x: Foo with Bar = $anon$1@62fb343d

scala> val t = typeOf(x)
t: reflect.runtime.universe.TypeTag[Foo with Bar] = TypeTag[Foo with Bar]

scala> t.tpe
res20: reflect.runtime.universe.Type = Foo with Bar

scala> t.tpe.toString
res21: String = Foo with Bar

And just to demonstrate that it yields the static type of the expression rather than the dynamic type of the object:

scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)

scala> val s:Seq[Int] = l
s: Seq[Int] = List(1, 2, 3)

scala> typeOf(s)
res22: reflect.runtime.universe.TypeTag[Seq[Int]] = TypeTag[scala.Seq[Int]]
查看更多
登录 后发表回答