Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile time type tracing

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
like image 613
Debilski Avatar asked Dec 09 '22 10:12

Debilski


1 Answers

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]

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

like image 89
James Iry Avatar answered Jan 06 '23 21:01

James Iry