In Scala 2.10, a new reflection library was introduced to add a more powerful toolkit of general reflective capabilities to Scala. Along with full-featured runtime reflection for Scala types and generics, Scala 2.10 also ships with compile-time reflection capabilities, in the form of macros, as well as the ability to reify Scala expressions into abstract syntax trees.
Questions tagged [scala-reflect]
278 questions
21
votes
1 answer
Getting subclasses of a sealed trait
Is it possible (via macros, some form of Shapeless automagic or otherwise) to obtain a list of the subclasses of a sealed trait:
At compile time?
At runtime?

NietzscheanAI
- 966
- 6
- 16
12
votes
5 answers
How to create a TypeTag manually?
I'm interested in creating a TypeTag manually (since 2.10M5):
object X {
import reflect.runtime.universe._
def tt[A : TypeTag](a: A) = typeTag[A] // how to do this manually?
val t = tt(List("")(_))
}
scalac -Xprint:typer .scala results…

kiritsuku
- 52,967
- 18
- 114
- 136
10
votes
1 answer
Constructing TypeTags of higher-kinded types
Given a simple parametrized type like class LK[A], I can write
// or simpler def tagLK[A: TypeTag] = typeTag[LK[A]]
def tagLK[A](implicit tA: TypeTag[A]) = typeTag[LK[A]]
tagLK[Int] == typeTag[LK[Int]] // true
Now I'd like to write an analogue for…

Alexey Romanov
- 167,066
- 35
- 309
- 487
9
votes
2 answers
In Scala Reflection, How to get generic type parameter of a concrete subclass?
Assuming that I have a Generic superclass:
class GenericExample[T](
a: String,
b: T
) {
def fn(i: T): T = b
}
and a concrete subclass:
case class Example(
…

tribbloid
- 4,026
- 14
- 64
- 103
8
votes
4 answers
Scala: reflection and case classes
The following code succeeds, but is there a better way of doing the same thing? Perhaps something specific to case classes? In the following code, for each field of type String in my simple case class, the code goes through my list of instances of…

Anthony Holland
- 161
- 11
8
votes
1 answer
Get a TypeTag from a Type?
I can get a Type from a TypeTag[A] by using the tpe method. But can I also recover the type-tag from a type?
import scala.reflect.runtime.{universe => ru}
import ru.{Type, TypeTag}
def forward[A](implicit tt: TypeTag[A]): Type = tt.tpe
def…

0__
- 66,707
- 21
- 171
- 266
8
votes
2 answers
Get TypeTag[A] from Class[A]
I have createOld method that I need to override and I cannot change it. I would like to use TypeTag to pattern match provided type in createNew. The goal is to find out how to call createNew from createOld. My current understanding is that compiler…

Rado Buransky
- 3,252
- 18
- 25
7
votes
1 answer
Scala - Run-time performance of TypeTags, ClassTags and WeakTypeTags
Introduction:
... TypeTag[T] encapsulates the runtime type representation of some compile-time type T. ...
... TypeTags are always generated by the compiler. ... [1]
TypeTags are located in scala.reflect.** packages.
Another SO answer…

mucaho
- 2,119
- 20
- 35
6
votes
1 answer
In Scala, how to create a TypeTag from a type that is serializable?
In Scala reflection, the TypeTag can usually be constructed from a Type using a TypeCreator:
object TypeUtils {
import ScalaReflection.universe._
def createTypeTag[T](
tpe: Type,
mirror:…

tribbloid
- 4,026
- 14
- 64
- 103
6
votes
1 answer
How to get the runtime value of parameter passed to a Scala macro?
I have an ostensibly simple macro problem that I’ve been banging my head against for a few hours, with no luck. Perhaps someone with more experience can help.
I have the following macro:
import scala.language.experimental.macros
import…

Dan Li
- 866
- 1
- 7
- 19
6
votes
1 answer
"illegal cyclic reference involving object InterfaceAudience" when using Scala 2.11 reflection
I am hitting the above error on scala 2.11.7:
def main(args: Array[String]): Unit = {
val x = typeOf[ org.apache.hadoop.io.Writable ]
println( x )
}
Additional info of my compilation process:
I've narrowed to the bare minimum necessary to…

Harel Gliksman
- 734
- 1
- 7
- 19
6
votes
1 answer
What are odd names like $read and $iw doing in the reified expression?
Here are some snippets from my Scala prompt. I import the reflection API and try reifying some expressions, as described in the docs here.
scala> import scala.reflect.runtime.{universe => ru}
scala> val str = "Duck I says."
scala>…

Rob N
- 15,024
- 17
- 92
- 165
5
votes
1 answer
Dynamic compilation of multiple Scala classes at runtime
I know I can compile individual "snippets" in Scala using the Toolbox like this:
import scala.reflect.runtime.universe
import scala.tools.reflect.ToolBox
object Compiler {
val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox()
…

John Reese
- 583
- 2
- 6
- 17
5
votes
0 answers
List class in package in compile time
I am trying to list all the class in a package in compile time.
Let's define a project with the following structure:
com.main
Main.scala
com.package
Method.scala
com.package.method1
Method1.scala
com.package.method2
…

ie8888
- 171
- 1
- 10
5
votes
2 answers
Why do we have to explicitly specify the ClassTag typeclass
Now that scala has iterated towards a JVM type erasure fix with the ClassTag typeclass, why is it an opt-in, rather than having the compiler always capture the type signature for runtime inspection. Having it an implicit parametrized type constraint…

Arne Claassen
- 14,088
- 5
- 67
- 106