This "virtual" case class represents the reflection interface for literal expressions which can not be further broken down or evaluated, such as "true", "0", "classOf[List]".
The API of Constant instances.
An extractor class to create and pattern match with syntax Constant(value)
where value
is the Scala value of the constant.
The constructor/extractor for Constant
instances.
The methods available for each reflection entity, without the implementation. Since the reflection entities are later overridden by runtime reflection and macros, their API counterparts guarantee a minimum set of methods that are implemented.
Extractors provide the machinery necessary to allow pattern matching and construction of reflection entities that is similar to case classes, although the entities are only abstract types that are later overridden.
EXPERIMENTAL
According to the section 6.24 "Constant Expressions" of the Scala language specification, certain expressions (dubbed constant expressions) can be evaluated by the Scala compiler at compile-time.
scala.reflect.api.Constants#Constant instances represent certain kinds of these expressions (with values stored in the
value
field and its strongly-typed views namedbooleanValue
,intValue
etc.), namely:Such constants are used to represent literals in abstract syntax trees (the scala.reflect.api.Trees#Literal node) and literal arguments for Java class file annotations (the scala.reflect.api.Annotations#LiteralArgument class).
Example
The
value
field deserves some explanation. Primitive and string values are represented as themselves, whereas references to classes and enums are a bit roundabout.Class references are represented as instances of scala.reflect.api.Types#Type (because when the Scala compiler processes a class reference, the underlying runtime class might not yet have been compiled). To convert such a reference to a runtime class, one should use the
runtimeClass
method of a mirror such as scala.reflect.api.Mirrors#RuntimeMirror (the simplest way to get such a mirror is using scala.reflect.runtime.package#currentMirror).Enumeration value references are represented as instances of scala.reflect.api.Symbols#Symbol, which on JVM point to methods that return underlying enum values. To inspect an underlying enumeration or to get runtime value of a reference to an enum, one should use a scala.reflect.api.Mirrors#RuntimeMirror (the simplest way to get such a mirror is again scala.reflect.runtime.package#currentMirror).