5
* <span class="badge badge-red" style="float: right;">EXPERIMENTAL</span>
7
* `Universe` provides a complete set of reflection operations which make it possible for one
8
* to reflectively inspect Scala type relations, such as membership or subtyping.
10
* [[scala.reflect.api.Universe]] has two specialized sub-universes for different scenarios.
11
* [[scala.reflect.api.JavaUniverse]] adds operations that link symbols and types to the underlying
12
* classes and runtime values of a JVM instance-- this can be thought of as the `Universe` that
13
* should be used for all typical use-cases of Scala reflection. [[scala.reflect.macros.Universe]]
14
* adds operations which allow macros to access selected compiler data structures and operations--
15
* this type of `Universe` should only ever exist within the implementation of a Scala macro.
17
* `Universe` can be thought of as the entry point to Scala reflection. It mixes-in, and thus provides
18
* an interface to the following main types:
20
* - [[scala.reflect.api.Types#Type Types]] represent types
21
* - [[scala.reflect.api.Symbols#Symbol Symbols]] represent definitions
22
* - [[scala.reflect.api.Trees#Tree Trees]] represent abstract syntax trees
23
* - [[scala.reflect.api.Names#Name Names]] represent term and type names
24
* - [[scala.reflect.api.Annotations#Annotation Annotations]] represent annotations
25
* - [[scala.reflect.api.Positions#Position Positions]] represent source positions of tree nodes
26
* - [[scala.reflect.api.FlagSets#FlagSet FlagSet]] represent sets of flags that apply to symbols and
28
* - [[scala.reflect.api.Constants#Constant Constants]] represent compile-time constants.
30
* To obtain a `Universe` to use with Scala runtime reflection, simply make sure to use or import
31
* `scala.reflect.runtime.universe._`
33
* scala> import scala.reflect.runtime.universe._
34
* import scala.reflect.runtime.universe._
36
* scala> typeOf[List[Int]]
37
* res0: reflect.runtime.universe.Type = scala.List[Int]
39
* scala> typeOf[Either[String, Int]]
40
* res1: reflect.runtime.universe.Type = scala.Either[String,Int]
43
* To obtain a `Universe` for use within a Scala macro, use [[scala.reflect.macros.Context#universe]]. For example:
45
* def printf(format: String, params: Any*): Unit = macro impl
46
* def impl(c: Context)(format: c.Expr[String], params: c.Expr[Any]*): c.Expr[Unit] = {
52
* For more information about `Universe`s, see the [[http://docs.scala-lang.org/overviews/reflection/environment-universes-mirrors.html Reflection Guide: Universes]]
54
* @groupprio Universe -1
55
* @group ReflectionAPI
57
* @contentDiagram hideNodes "*Api"
59
abstract class Universe extends Symbols
71
with StandardDefinitions
78
/** Use `reify` to produce the abstract syntax tree representing a given Scala expression.
83
* val five = reify{ 5 } // Literal(Constant(5))
84
* reify{ 5.toString } // Apply(Select(Literal(Constant(5)), TermName("toString")), List())
85
* reify{ five.splice.toString } // Apply(Select(five, TermName("toString")), List())
88
* The produced tree is path dependent on the Universe `reify` was called from.
90
* Use [[scala.reflect.api.Exprs#Expr.splice]] to embed an existing expression into a `reify` call. Use [[Expr]] to turn a [[Tree]] into an expression that can be spliced.
93
// implementation is hardwired to `scala.reflect.reify.Taggers`
94
// using the mechanism implemented in `scala.tools.reflect.FastTrack`
95
def reify[T](expr: T): Expr[T] = ??? // macro