Skip to content

Commit ae6fb30

Browse files
committed
Add quoted test for flexible types
1 parent 40868c1 commit ae6fb30

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Inlined(None, Nil, Literal(StringConstant("hello")))
2+
ConstantType(StringConstant("hello"))
3+
4+
Inlined(None, Nil, Apply(Select(Literal(StringConstant("world")), "trim"), Nil))
5+
FlexibleType(TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "String"))
6+
7+
FlexibleType(TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "java")), "lang"), "String"))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import scala.quoted.*
2+
3+
object Macros {
4+
5+
implicit inline def printTree[T](inline x: T): Unit =
6+
${ impl('x) }
7+
8+
def impl[T](x: Expr[T])(using Quotes) : Expr[Unit] = {
9+
import quotes.reflect.*
10+
11+
val tree = x.asTerm
12+
val treeStr = Expr(tree.show(using Printer.TreeStructure))
13+
val treeTpeStr = Expr(tree.tpe.show(using Printer.TypeReprStructure))
14+
15+
'{
16+
println(${treeStr})
17+
println(${treeTpeStr})
18+
println()
19+
}
20+
}
21+
22+
inline def theTestBlock : Unit = ${ theTestBlockImpl }
23+
24+
def theTestBlockImpl(using qctx : Quotes) : Expr[Unit] = {
25+
import quotes.reflect.*
26+
27+
val ft1 = FlexibleType(TypeRepr.of[String])
28+
val ft1e = Expr(ft1.show(using Printer.TypeReprStructure))
29+
30+
'{
31+
println(${ft1e})
32+
}
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import Macros.*
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
printTree("hello")
7+
printTree("world".trim())
8+
theTestBlock
9+
}
10+
}

0 commit comments

Comments
 (0)