diff --git a/tests/run-macros/i10914a/Macro_1.scala b/tests/run-macros/i10914a/Macro_1.scala new file mode 100644 index 000000000000..c86304c42a86 --- /dev/null +++ b/tests/run-macros/i10914a/Macro_1.scala @@ -0,0 +1,30 @@ +import scala.quoted._ + +case class Entity(value: String) +case class Input(ent: Entity) +case class Container(ents: List[Entity]) + +object Dsl { + inline def container(inline c: Input):Container = ${ containerImpl('c) } + def containerImpl(c: Expr[Input])(using Quotes): Expr[Container] = + import quotes.reflect._ + val entExpr = c match + case '{ Input($ent) } => ent + case _ => report.throwError("Cannot Extract Entity from Input") + '{ Container(List($entExpr)) } + + + given FromExpr[Entity] with + def unapply(expr: Expr[Entity])(using Quotes) = expr match + case '{ Entity(${Expr(value)}) } => Some(Entity(value)) + case _ => None + + inline def pull(inline c: Container): Entity = ${ pullImpl('c) } + def pullImpl(c: Expr[Container])(using Quotes): Expr[Entity] = + import quotes.reflect._ + val inputs = c match + case '{ Container($list) } => + list.valueOrError + case _ => report.throwError("Cannot Extract List from Container") + '{ Entity(${Expr(inputs.head.value)}) } +} diff --git a/tests/run-macros/i10914a/Test_2.scala b/tests/run-macros/i10914a/Test_2.scala new file mode 100644 index 000000000000..a93784fb2b07 --- /dev/null +++ b/tests/run-macros/i10914a/Test_2.scala @@ -0,0 +1,16 @@ +object Test { + import Dsl._ + + inline def makeEnt = Entity("foo") + + // inline def entity = makeEnt // This case breaks to + //inline def input = Input(entity) + + inline def input = Input(makeEnt) + inline def contained = container(input) + inline def output = pull(contained) + + def main(args: Array[String]): Unit = { + println( output ) + } +} diff --git a/tests/run-macros/i10914b/Macro_1.scala b/tests/run-macros/i10914b/Macro_1.scala new file mode 100644 index 000000000000..0515a4eb8cf3 --- /dev/null +++ b/tests/run-macros/i10914b/Macro_1.scala @@ -0,0 +1,34 @@ +import scala.quoted._ + +case class Entity(value: String) +case class Input(ent: Entity) +case class Container(ents: List[Entity]) + +object Dsl { + def makeEnt = Entity("foo") + + inline def container(inline c: Input):Container = ${ containerImpl('c) } + def containerImpl(c: Expr[Input])(using Quotes): Expr[Container] = + import quotes.reflect._ + //println("Getting Input: " + Printer.TreeStructure.show(c.asTerm)) + val entExpr = c match + case '{ Input($ent) } => ent + case _ => report.throwError("Cannot Extract Entity from Input") + '{ Container(List($entExpr)) } + + + given FromExpr[Entity] with + def unapply(expr: Expr[Entity])(using Quotes) = expr match + case '{ Entity(${Expr(value)}) } => Some(Entity(value)) + case '{ makeEnt } => Some(makeEnt) + case _ => None + + inline def pull(inline c: Container): Entity = ${ pullImpl('c) } + def pullImpl(c: Expr[Container])(using Quotes): Expr[Entity] = + import quotes.reflect._ + val inputs = c match + case '{ Container($list) } => + list.valueOrError + case _ => report.throwError("Cannot Extract List from Container") + '{ Entity(${Expr(inputs.head.value)}) } +} diff --git a/tests/run-macros/i10914b/Test_2.scala b/tests/run-macros/i10914b/Test_2.scala new file mode 100644 index 000000000000..790b6ef1c99e --- /dev/null +++ b/tests/run-macros/i10914b/Test_2.scala @@ -0,0 +1,14 @@ +object Test { + import Dsl._ + + // inline def entity = makeEnt // This case breaks to + //inline def input = Input(entity) + + inline def input = Input(makeEnt) + inline def contained = container(input) + inline def output = pull(contained) + + def main(args: Array[String]): Unit = { + println( output ) + } +}