Skip to content

Commit 4e3f290

Browse files
committed
Merge pull request scala#3636 from xeno-by/topic/mirror-typeof
introduces Mirror.typeOf
2 parents 49f54ab + f9a5880 commit 4e3f290

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

bincompat-backward.whitelist.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ filter {
132132
{
133133
matchName="scala.reflect.api.Internals#ReificationSupportApi.SyntacticPartialFunction"
134134
problemName=MissingMethodProblem
135+
},
136+
{
137+
matchName="scala.reflect.api.Mirror.symbolOf"
138+
problemName=MissingMethodProblem
139+
},
140+
{
141+
matchName="scala.reflect.api.Mirror.typeOf"
142+
problemName=MissingMethodProblem
143+
},
144+
{
145+
matchName="scala.reflect.api.Mirror.weakTypeOf"
146+
problemName=MissingMethodProblem
135147
}
136148
]
137149
}

bincompat-forward.whitelist.conf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ filter {
111111
matchName="scala.reflect.api.StandardLiftables#StandardLiftableInstances.liftTree"
112112
problemName=MissingMethodProblem
113113
},
114-
// see SI-8331
114+
// see SI-8331
115115
{
116116
matchName="scala.reflect.api.Internals#ReificationSupportApi.SyntacticSelectType"
117117
problemName=MissingMethodProblem
@@ -152,6 +152,18 @@ filter {
152152
{
153153
matchName="scala.collection.Iterator#ConcatIterator.this"
154154
problemName=MissingMethodProblem
155+
},
156+
{
157+
matchName="scala.reflect.api.Mirror.symbolOf"
158+
problemName=MissingMethodProblem
159+
},
160+
{
161+
matchName="scala.reflect.api.Mirror.typeOf"
162+
problemName=MissingMethodProblem
163+
},
164+
{
165+
matchName="scala.reflect.api.Mirror.weakTypeOf"
166+
problemName=MissingMethodProblem
155167
}
156168
]
157169
}

src/reflect/scala/reflect/api/Mirror.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,22 @@ abstract class Mirror[U <: Universe with Singleton] {
118118
* @group Mirror
119119
*/
120120
def staticPackage(fullName: String): U#ModuleSymbol
121+
122+
/**
123+
* Shortcut for `implicitly[WeakTypeTag[T]].tpe`
124+
* @group TypeTags
125+
*/
126+
def weakTypeOf[T: universe.WeakTypeTag]: U#Type = universe.weakTypeTag[T].in(this).tpe
127+
128+
/**
129+
* Shortcut for `implicitly[TypeTag[T]].tpe`
130+
* @group TypeTags
131+
*/
132+
def typeOf[T: universe.TypeTag]: U#Type = universe.typeTag[T].in(this).tpe
133+
134+
/**
135+
* Type symbol of `x` as derived from a type tag.
136+
* @group TypeTags
137+
*/
138+
def symbolOf[T: universe.WeakTypeTag]: U#TypeSymbol
121139
}

src/reflect/scala/reflect/internal/Mirrors.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ trait Mirrors extends api.Mirrors {
3030
val EmptyPackageClass: ClassSymbol
3131
val EmptyPackage: ModuleSymbol
3232

33+
def symbolOf[T: universe.WeakTypeTag]: universe.TypeSymbol = universe.weakTypeTag[T].in(this).tpe.typeSymbolDirect.asType
34+
3335
def findMemberFromRoot(fullName: Name): Symbol = {
3436
val segs = nme.segments(fullName.toString, fullName.isTermName)
3537
if (segs.isEmpty) NoSymbol
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Int
2+
object C
3+
type T
4+
type Id
5+
class Nothing
6+
class Null
7+
class Int
8+
object C
9+
type T
10+
type Id
11+
class Nothing
12+
class Null
13+
exception: class C not found.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import scala.reflect.runtime.universe._
2+
import scala.reflect.runtime.{universe => ru}
3+
import scala.reflect.runtime.{currentMirror => cm}
4+
import scala.reflect.api.Mirror
5+
6+
class C
7+
object C
8+
9+
object Test extends App {
10+
object test1 {
11+
val m = cm
12+
type T = Int
13+
type Id[X] = X
14+
println(m.symbolOf[Int]: ru.TypeSymbol)
15+
println(m.symbolOf[C.type]: ru.TypeSymbol)
16+
println(m.symbolOf[T]: ru.TypeSymbol)
17+
println(m.symbolOf[Id[_]]: ru.TypeSymbol)
18+
println(m.symbolOf[Nothing]: ru.TypeSymbol)
19+
println(m.symbolOf[Null]: ru.TypeSymbol)
20+
}
21+
22+
object test2 {
23+
val m: Mirror[ru.type] = cm
24+
type T = Int
25+
type Id[X] = X
26+
println(m.symbolOf[Int]: ru.TypeSymbol)
27+
println(m.symbolOf[C.type]: ru.TypeSymbol)
28+
println(m.symbolOf[T]: ru.TypeSymbol)
29+
println(m.symbolOf[Id[_]]: ru.TypeSymbol)
30+
println(m.symbolOf[Nothing]: ru.TypeSymbol)
31+
println(m.symbolOf[Null]: ru.TypeSymbol)
32+
}
33+
34+
object test3 {
35+
val m = ru.runtimeMirror(classOf[Int].getClass.getClassLoader)
36+
try println(m.symbolOf[C])
37+
catch { case ex: ScalaReflectionException => println(s"exception: ${ex.getMessage}") }
38+
}
39+
40+
test1
41+
test2
42+
test3
43+
}

0 commit comments

Comments
 (0)