Get TypeTag[A] from Class[A]

It is possible to create a TypeTag from a Class using Scala reflection, though I’m not sure if this implementation of TypeCreator is absolutely correct: import scala.reflect.runtime.universe._ def createOld[A](c: Class[A]): A = createNew { val mirror = runtimeMirror(c.getClassLoader) // obtain runtime mirror val sym = mirror.staticClass(c.getName) // obtain class symbol for `c` val tpe = … Read more

Get a TypeTag from a Type?

It is possible: import scala.reflect.runtime.universe._ import scala.reflect.api val mirror = runtimeMirror(getClass.getClassLoader) // whatever mirror you use to obtain the `Type` def backward[T](tpe: Type): TypeTag[T] = TypeTag(mirror, new api.TypeCreator { def apply[U <: api.Universe with Singleton](m: api.Mirror[U]) = if (m eq mirror) tpe.asInstanceOf[U # Type] else throw new IllegalArgumentException(s”Type tag defined in $mirror cannot be migrated … Read more

In scala 2 or 3, is it possible to debug implicit resolution process in runtime?

You can debug implicits at compile time: switch on compiler flag -Xlog-implicits try to resolve implicits manually (maybe specifying type parameters as well) and see compile errors implicitly[…](…manually…) use scala.reflect println(reify { implicitly[…] }.tree) (or switch on compiler flag -Xprint:typer) in order to see how implicits are resolved use IDE functionality to show implicits using … Read more