File tree Expand file tree Collapse file tree 4 files changed +94
-0
lines changed
Expand file tree Collapse file tree 4 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1+ import scala .quoted ._
2+
3+ case class Entity (value : String )
4+ case class Input (ent : Entity )
5+ case class Container (ents : List [Entity ])
6+
7+ object Dsl {
8+ inline def container (inline c : Input ): Container = $ { containerImpl(' c ) }
9+ def containerImpl (c : Expr [Input ])(using Quotes ): Expr [Container ] =
10+ import quotes .reflect ._
11+ val entExpr = c match
12+ case ' { Input ($ent) } => ent
13+ case _ => report.throwError(" Cannot Extract Entity from Input" )
14+ ' { Container (List ($entExpr)) }
15+
16+
17+ given FromExpr [Entity ] with
18+ def unapply (expr : Expr [Entity ])(using Quotes ) = expr match
19+ case ' { Entity ($ {Expr (value)}) } => Some (Entity (value))
20+ case _ => None
21+
22+ inline def pull (inline c : Container ): Entity = $ { pullImpl(' c ) }
23+ def pullImpl (c : Expr [Container ])(using Quotes ): Expr [Entity ] =
24+ import quotes .reflect ._
25+ val inputs = c match
26+ case ' { Container ($list) } =>
27+ list.valueOrError
28+ case _ => report.throwError(" Cannot Extract List from Container" )
29+ ' { Entity ($ {Expr (inputs.head.value)}) }
30+ }
Original file line number Diff line number Diff line change 1+ object Test {
2+ import Dsl ._
3+
4+ inline def makeEnt = Entity (" foo" )
5+
6+ // inline def entity = makeEnt // This case breaks to
7+ // inline def input = Input(entity)
8+
9+ inline def input = Input (makeEnt)
10+ inline def contained = container(input)
11+ inline def output = pull(contained)
12+
13+ def main (args : Array [String ]): Unit = {
14+ println( output )
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ import scala .quoted ._
2+
3+ case class Entity (value : String )
4+ case class Input (ent : Entity )
5+ case class Container (ents : List [Entity ])
6+
7+ object Dsl {
8+ def makeEnt = Entity (" foo" )
9+
10+ inline def container (inline c : Input ): Container = $ { containerImpl(' c ) }
11+ def containerImpl (c : Expr [Input ])(using Quotes ): Expr [Container ] =
12+ import quotes .reflect ._
13+ // println("Getting Input: " + Printer.TreeStructure.show(c.asTerm))
14+ val entExpr = c match
15+ case ' { Input ($ent) } => ent
16+ case _ => report.throwError(" Cannot Extract Entity from Input" )
17+ ' { Container (List ($entExpr)) }
18+
19+
20+ given FromExpr [Entity ] with
21+ def unapply (expr : Expr [Entity ])(using Quotes ) = expr match
22+ case ' { Entity ($ {Expr (value)}) } => Some (Entity (value))
23+ case ' { makeEnt } => Some (makeEnt)
24+ case _ => None
25+
26+ inline def pull (inline c : Container ): Entity = $ { pullImpl(' c ) }
27+ def pullImpl (c : Expr [Container ])(using Quotes ): Expr [Entity ] =
28+ import quotes .reflect ._
29+ val inputs = c match
30+ case ' { Container ($list) } =>
31+ list.valueOrError
32+ case _ => report.throwError(" Cannot Extract List from Container" )
33+ ' { Entity ($ {Expr (inputs.head.value)}) }
34+ }
Original file line number Diff line number Diff line change 1+ object Test {
2+ import Dsl ._
3+
4+ // inline def entity = makeEnt // This case breaks to
5+ // inline def input = Input(entity)
6+
7+ inline def input = Input (makeEnt)
8+ inline def contained = container(input)
9+ inline def output = pull(contained)
10+
11+ def main (args : Array [String ]): Unit = {
12+ println( output )
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments