diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2014-01-28 12:05:34 +0300 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2014-02-14 14:09:17 +0100 |
commit | ada0252d4494611904c15cc5da72654c1a180a8f (patch) | |
tree | 662e972fdf295e57bdb5bcc6b47aafec7a242ea8 /src | |
parent | b0176294060c9ce8b86d71e7bc8a7a13cef15b1e (diff) | |
download | scala-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.
Diffstat (limited to 'src')
-rw-r--r-- | src/reflect/scala/reflect/api/TypeTags.scala | 6 | ||||
-rw-r--r-- | src/reflect/scala/reflect/internal/Symbols.scala | 2 | ||||
-rw-r--r-- | src/reflect/scala/reflect/macros/Aliases.scala | 5 |
3 files changed, 13 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] } |