summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2014-01-28 12:05:34 +0300
committerEugene Burmako <xeno.by@gmail.com>2014-02-14 14:09:17 +0100
commitada0252d4494611904c15cc5da72654c1a180a8f (patch)
tree662e972fdf295e57bdb5bcc6b47aafec7a242ea8
parentb0176294060c9ce8b86d71e7bc8a7a13cef15b1e (diff)
downloadscala-ada0252d4494611904c15cc5da72654c1a180a8f.tar.gz
scala-ada0252d4494611904c15cc5da72654c1a180a8f.tar.bz2
scala-ada0252d4494611904c15cc5da72654c1a180a8f.zip
SI-8194 adds Universe.symbolOf[T]
A very frequent use for a result of typeOf is obtaining an underlying type symbol. Another thing that comes up occasionally at stack overflow is a request to add facilities for reification of symbols. This naturally suggests that our reflection API would benefit from a method called symbolOf that can take a term or type argument and return an underlying symbol. While an API to extract a term symbol from an expression needs some time to be designed and then implemented robustly (we don’t have untyped macros so we’ll have to account for various desugarings), meaning that we probably won’t have time for that in 2.11, a type symbol extractor seems to be a very low-hanging fruit.
-rw-r--r--src/reflect/scala/reflect/api/TypeTags.scala6
-rw-r--r--src/reflect/scala/reflect/internal/Symbols.scala2
-rw-r--r--src/reflect/scala/reflect/macros/Aliases.scala5
-rw-r--r--test/files/run/typetags_symbolof_x.check6
-rw-r--r--test/files/run/typetags_symbolof_x.scala15
5 files changed, 34 insertions, 0 deletions
diff --git a/src/reflect/scala/reflect/api/TypeTags.scala b/src/reflect/scala/reflect/api/TypeTags.scala
index d6af68f923..1d5bf5d28b 100644
--- a/src/reflect/scala/reflect/api/TypeTags.scala
+++ b/src/reflect/scala/reflect/api/TypeTags.scala
@@ -345,6 +345,12 @@ trait TypeTags { self: Universe =>
* @group TypeTags
*/
def typeOf[T: TypeTag](x: => T): Type = typeOf[T]
+
+ /**
+ * Type symbol of `x` as derived from a type tag.
+ * @group TypeTags
+ */
+ def symbolOf[T: WeakTypeTag]: TypeSymbol
}
private[scala] class SerializedTypeTag(var tpec: TypeCreator, var concrete: Boolean) extends Serializable {
diff --git a/src/reflect/scala/reflect/internal/Symbols.scala b/src/reflect/scala/reflect/internal/Symbols.scala
index 94b60bafae..31fb7d2a6e 100644
--- a/src/reflect/scala/reflect/internal/Symbols.scala
+++ b/src/reflect/scala/reflect/internal/Symbols.scala
@@ -77,6 +77,8 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
}
}
+ def symbolOf[T: WeakTypeTag]: TypeSymbol = weakTypeOf[T].typeSymbolDirect.asType
+
abstract class SymbolContextApiImpl extends SymbolContextApi {
this: Symbol =>
diff --git a/src/reflect/scala/reflect/macros/Aliases.scala b/src/reflect/scala/reflect/macros/Aliases.scala
index 8651661c63..bd918bbe56 100644
--- a/src/reflect/scala/reflect/macros/Aliases.scala
+++ b/src/reflect/scala/reflect/macros/Aliases.scala
@@ -126,4 +126,9 @@ trait Aliases {
* Type of `x` as derived from a type tag.
*/
def typeOf[T: TypeTag](x: => T): Type = typeOf[T]
+
+ /**
+ * Type symbol of `x` as derived from a type tag.
+ */
+ def symbolOf[T: WeakTypeTag]: universe.TypeSymbol = universe.symbolOf[T]
}
diff --git a/test/files/run/typetags_symbolof_x.check b/test/files/run/typetags_symbolof_x.check
new file mode 100644
index 0000000000..fd0e069bca
--- /dev/null
+++ b/test/files/run/typetags_symbolof_x.check
@@ -0,0 +1,6 @@
+class Int
+object C
+type T
+type Id
+class Nothing
+class Null
diff --git a/test/files/run/typetags_symbolof_x.scala b/test/files/run/typetags_symbolof_x.scala
new file mode 100644
index 0000000000..333c4e7da4
--- /dev/null
+++ b/test/files/run/typetags_symbolof_x.scala
@@ -0,0 +1,15 @@
+import scala.reflect.runtime.universe._
+
+class C
+object C
+
+object Test extends App {
+ type T = Int
+ type Id[X] = X
+ println(symbolOf[Int])
+ println(symbolOf[C.type])
+ println(symbolOf[T])
+ println(symbolOf[Id[_]])
+ println(symbolOf[Nothing])
+ println(symbolOf[Null])
+}