Skip to content

Commit c9ccb21

Browse files
committed
Add Valuable test case
1 parent e17ef67 commit c9ccb21

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

tests/run/tasty-eval.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Some(1)
2+
Some(8)
3+
Some(5)
4+
Some(6)
5+
None

tests/run/tasty-eval/quoted_1.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import scala.quoted._
2+
3+
import scala.tasty.Universe
4+
import scala.tasty.util.{TastyPrinter, TreeTraverser}
5+
6+
object Macros {
7+
8+
implicit inline def foo(i: Int): String =
9+
~impl('(i))(Universe.compilationUniverse) // FIXME infer Universe.compilationUniverse within top level ~
10+
11+
def impl(i: Expr[Int])(implicit u: Universe): Expr[String] = {
12+
value(i).toString.toExpr
13+
}
14+
15+
inline implicit def value[X](e: Expr[X])(implicit u: Universe, ev: Valuable[X]): Option[X] = ev.value(e)
16+
17+
trait Valuable[X] {
18+
def value(e: Expr[X])(implicit u: Universe): Option[X]
19+
}
20+
21+
implicit def intIsEvalable: Valuable[Int] = new Valuable[Int] {
22+
override def value(e: Expr[Int])(implicit u: Universe): Option[Int] = {
23+
import u._
24+
import u.tasty._
25+
e.toTasty.tpe match {
26+
case SymRef(ValDef(_, tpt, _), pre) =>
27+
tpt.tpe match {
28+
case ConstantType(IntConstant(i)) => Some(i)
29+
case _ => None
30+
}
31+
case ConstantType(IntConstant(i)) => Some(i)
32+
case _ => None
33+
}
34+
}
35+
}
36+
}

tests/run/tasty-eval/quoted_2.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
import Macros._
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
println(foo(1)) // "Some(1)"
7+
println(foo(1 + 7)) // "Some(8)"
8+
final val y = 5
9+
println(foo(y)) // "Some(5)"
10+
println(foo(y + 1))
11+
val x = 4
12+
println(foo(x)) // "None"
13+
}
14+
}

0 commit comments

Comments
 (0)