本文介绍了编译时间类型跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Scala表达式周围添加一些魔术构造,以便在编译过程中打印出类型?例如.有一些类,魔术函数,元编程类型,可以做到:

Is it possible to add some magic construct around a Scala expression so that it prints the type during compilation? E.g. have some class, magic function, meta programming type, which does:

val i = 1
Some(11).map(Trace(_ + 1))

// compile
// prints: Int

推荐答案

不完全是,但是如何做到这一点

Not exactly, but how 'bout this

$ cat Test.scala
def Trace[T] = identity[T] _

val i = 1
Some(11) map {x => Trace(x + 1)}



$ scala -Xprint:typer Test.scala 2>&1 | egrep --o 'Trace\[.*\]'
Trace[T >: Nothing <: Any]
Trace[Int]

第一个跟踪来自跟踪的定义,可以忽略.同样的参数(-Xprint:typer)也适用于scalac.

The first Trace comes from the definition of Trace and can be ignored. The same parameter (-Xprint:typer) works with scalac, too.

这篇关于编译时间类型跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:41