You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* According to the section 6.24 "Constant Expressions" of the Scala language specification,
21
21
* certain expressions (dubbed ''constant expressions'') can be evaluated by the Scala compiler at compile-time.
22
+
* Examples include "true", "0" and "classOf[List]".
22
23
*
23
-
* [[scala.reflect.api.Constants#Constant]] instances represent certain kinds of these expressions
24
-
* (with values stored in the `value` field and its strongly-typed views named `booleanValue`, `intValue` etc.), namely:
25
-
* 1. Literals of primitive value classes (bytes, shorts, ints, longs, floats, doubles, chars, booleans and voids).
26
-
* 1. String literals.
27
-
* 1. References to classes (typically constructed with [[scala.Predef#classOf]]).
28
-
* 1. References to enumeration values.
24
+
* `Constant` instances can be matched against and can be constructed directly, as if they were case classes:
25
+
* {{{
26
+
* assert(Constant(true).value == true)
27
+
* Constant(true) match {
28
+
* case Constant(s: String) => println("A string: " + s)
29
+
* case Constant(b: Boolean) => println("A boolean value: " + b)
30
+
* case Constant(x) => println("Something else: " + x)
31
+
* }
32
+
* }}}
29
33
*
30
-
* Such constants are used to represent literals in abstract syntax trees (the [[scala.reflect.api.Trees#Literal]] node)
31
-
* and literal arguments for Java class file annotations (the [[scala.reflect.api.Annotations#LiteralArgument]] class).
34
+
* `Constant` instances can wrap the following kinds of expressions:
35
+
* 1. Literals of primitive value classes ([[scala.Byte `Byte`]], [[scala.Short `Short`]], [[scala.Int `Int`]], [[scala.Long `Long`]], [[scala.Float `Float`]], [[scala.Double `Double`]], [[scala.Char `Char`]], [[scala.Boolean `Boolean`]] and [[scala.Unit `Unit`]]) - represented directly as the corresponding type
36
+
* 1. String literals - represented as instances of `String`.
37
+
* 1. References to classes, typically constructed with [[scala.Predef#classOf]] - represented as [[scala.reflect.api.Types#Type types]].
38
+
* 1. References to enumeration values - represented as [[scala.reflect.api.Symbols#Symbol symbols]].
39
+
*
40
+
* Instances are used to represent literals in abstract syntax trees, inside [[scala.reflect.api.Trees#Literal]] nodes.
32
41
*
33
42
* === Example ===
34
43
*
@@ -43,10 +52,12 @@ package api
43
52
* Enumeration value references are represented as instances of [[scala.reflect.api.Symbols#Symbol]], which on JVM point to methods
44
53
* that return underlying enum values. To inspect an underlying enumeration or to get runtime value of a reference to an enum,
45
54
* one should use a [[scala.reflect.api.Mirrors#RuntimeMirror]] (the simplest way to get such a mirror is again [[scala.reflect.runtime.package#currentMirror]]).
* println(cm.runtimeClass(classRef)) // class JavaAnnottee
88
+
* // enum value reference, cast to Symbol
89
+
* val enumRef = jArg[Symbol]("enumRef").get
90
+
* println(enumRef) // value BAR
91
+
*
92
+
* val siblings = enumRef.owner.info.decls
93
+
* val enumValues = siblings.filter(_.isJavaEnum)
94
+
* println(enumValues) // Scope{
95
+
* // final val FOO: JavaSimpleEnumeration;
96
+
* // final val BAR: JavaSimpleEnumeration
97
+
* // }
98
+
*
99
+
* // doesn't work because of https://github.com/scala/bug/issues/6459
100
+
* // val enumValue = mirror.reflectField(enumRef.asTerm).get
101
+
* val enumClass = cm.runtimeClass(enumRef.owner.asClass)
102
+
* val enumValue = enumClass.getDeclaredField(enumRef.name.toString).get(null)
103
+
* println(enumValue) // BAR
92
104
* }}}
93
105
*
94
106
* @contentDiagram hideNodes "*Api"
@@ -97,97 +109,6 @@ package api
97
109
traitConstants {
98
110
self: Universe=>
99
111
100
-
/**
101
-
* This "virtual" case class represents the reflection interface for literal expressions which can not be further
102
-
* broken down or evaluated, such as "true", "0", "classOf[List]". Such values become parts of the Scala abstract
103
-
* syntax tree representing the program. The constants
104
-
* correspond to section 6.24 "Constant Expressions" of the
105
-
* [[http://www.scala-lang.org/files/archive/spec/2.13/ Scala Language Specification]].
106
-
*
107
-
* Such constants are used to represent literals in abstract syntax trees (the [[scala.reflect.api.Trees#Literal]] node)
108
-
* and literal arguments for Java class file annotations (the [[scala.reflect.api.Annotations#LiteralArgument]] class).
109
-
*
110
-
* Constants can be matched against and can be constructed directly, as if they were case classes:
111
-
* {{{
112
-
* assert(Constant(true).value == true)
113
-
* Constant(true) match {
114
-
* case Constant(s: String) => println("A string: " + s)
115
-
* case Constant(b: Boolean) => println("A boolean value: " + b)
116
-
* case Constant(x) => println("Something else: " + x)
117
-
* }
118
-
* }}}
119
-
*
120
-
* `Constant` instances can wrap certain kinds of these expressions:
121
-
* 1. Literals of primitive value classes ([[scala.Byte `Byte`]], [[scala.Short `Short`]], [[scala.Int `Int`]], [[scala.Long `Long`]], [[scala.Float `Float`]], [[scala.Double `Double`]], [[scala.Char `Char`]], [[scala.Boolean `Boolean`]] and [[scala.Unit `Unit`]]) - represented directly as the corresponding type
122
-
* 1. String literals - represented as instances of the `String`.
123
-
* 1. References to classes, typically constructed with [[scala.Predef#classOf]] - represented as [[scala.reflect.api.Types#Type types]].
124
-
* 1. References to enumeration values - represented as [[scala.reflect.api.Symbols#Symbol symbols]].
125
-
*
126
-
* Class references are represented as instances of [[scala.reflect.api.Types#Type]]
127
-
* (because when the Scala compiler processes a class reference, the underlying runtime class might not yet have
128
-
* been compiled). To convert such a reference to a runtime class, one should use the [[scala.reflect.api.Mirrors#RuntimeMirror#runtimeClass `runtimeClass`]] method of a
129
-
* mirror such as [[scala.reflect.api.Mirrors#RuntimeMirror `RuntimeMirror`]] (the simplest way to get such a mirror is using
* Enumeration value references are represented as instances of [[scala.reflect.api.Symbols#Symbol]], which on JVM point to methods
133
-
* that return underlying enum values. To inspect an underlying enumeration or to get runtime value of a reference to an enum,
134
-
* one should use a [[scala.reflect.api.Mirrors#RuntimeMirror]] (the simplest way to get such a mirror is again [[scala.reflect.runtime.package#currentMirror]]).
0 commit comments