Skip to content

Commit 359ea50

Browse files
committed
Add typeOf to TASTy reflection
1 parent f787d11 commit 359ea50

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

library/src/scala/tasty/Reflection.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ abstract class Reflection
2020
with TreeOps
2121
with TreeUtils
2222
with TypeOrBoundsTreeOps
23-
with TypeOrBoundsOps
23+
with TypeOrBoundsOps {
24+
25+
def typeOf[T: scala.quoted.Type]: Type =
26+
implicitly[scala.quoted.Type[T]].unseal.tpe
27+
}
2428

2529
object Reflection {
2630
/** Compiler tasty context available in a top level ~ of an inline macro */

library/src/scala/tasty/reflect/TypeOrBoundsOps.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ trait TypeOrBoundsOps extends Core {
55

66
// ----- Types ----------------------------------------------------
77

8+
def typeOf[T: scala.quoted.Type]: Type
9+
810
trait TypeAPI {
911
def =:=(other: Type)(implicit ctx: Context): Boolean
1012
def <:<(other: Type)(implicit ctx: Context): Boolean
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
collection.immutable.List[scala.Int]
2+
Macros
3+
Macros
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import scala.quoted._
2+
import scala.tasty._
3+
4+
object Macros {
5+
6+
inline def testTypeOf(): Unit = ~testTypeOfImpl
7+
8+
private def testTypeOfImpl(implicit reflect: Reflection): Expr[Unit] = {
9+
import reflect._
10+
'{
11+
assert(~(typeOf[Unit] =:= definitions.UnitType).toExpr, "Unit")
12+
assert(~(typeOf[Byte] =:= definitions.ByteType).toExpr, "Byte")
13+
assert(~(typeOf[Short] =:= definitions.ShortType).toExpr, "Short")
14+
assert(~(typeOf[Int] =:= definitions.IntType).toExpr, "Int")
15+
assert(~(typeOf[Long] =:= definitions.LongType).toExpr, "Long")
16+
assert(~(typeOf[Float] =:= definitions.FloatType).toExpr, "Float")
17+
assert(~(typeOf[Double] =:= definitions.DoubleType).toExpr, "Double")
18+
assert(~(typeOf[Char] =:= definitions.CharType).toExpr, "Char")
19+
assert(~(typeOf[String] =:= definitions.StringType).toExpr, "String")
20+
21+
assert(~(typeOf[Any] =:= definitions.AnyType).toExpr, "Any")
22+
assert(~(typeOf[AnyRef] =:= definitions.AnyRefType).toExpr, "AnyRef")
23+
assert(~(typeOf[AnyVal] =:= definitions.AnyValType).toExpr, "AnyVal")
24+
assert(~(typeOf[Object] =:= definitions.ObjectType).toExpr, "Object")
25+
assert(~(typeOf[Nothing] =:= definitions.NothingType).toExpr, "Nothing")
26+
27+
println(~(typeOf[List[Int]].showCode.toExpr))
28+
println(~(typeOf[Macros].showCode.toExpr))
29+
println(~(typeOf[Macros.type].showCode.toExpr))
30+
}
31+
}
32+
33+
}
34+
35+
class Macros
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
object Test {
3+
4+
def main(args: Array[String]): Unit = {
5+
Macros.testTypeOf()
6+
}
7+
8+
}

0 commit comments

Comments
 (0)