File tree 3 files changed +49
-0
lines changed
3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ bar codec
2
+ foo codec
Original file line number Diff line number Diff line change
1
+ //> using options -experimental
2
+
3
+ import scala .quoted .*
4
+
5
+ trait Codec [- T ] { def print (): Unit }
6
+ object Codec {
7
+ inline def derivedWithDeps [T ](deps : Any ): Codec [T ] = $ {derivedWithDepsImpl[T ](' deps )}
8
+
9
+ private def derivedWithDepsImpl [T ](deps : Expr [Any ])(using q : Quotes )(using Type [T ]): Expr [Codec [T ]] = {
10
+ import q .reflect .*
11
+
12
+ val givenSelector : Selector = GivenSelector (None )
13
+ val theImport = Import (deps.asTerm, List (givenSelector))
14
+ Block (List (theImport), ' {scala.compiletime.summonInline[Codec [T ]]}.asTerm).asExprOf[Codec [T ]]
15
+ /* import deps.given
16
+ * summonInline[Codec[T]]
17
+ */
18
+ }
19
+
20
+ inline def derivedWithDepsWithNamedOmitted [T ](deps : Any ): Codec [T ] = $ {derivedWithDepsWithNamedOmittedImpl[T ](' deps )}
21
+
22
+ private def derivedWithDepsWithNamedOmittedImpl [T ](deps : Expr [Any ])(using q : Quotes )(using Type [T ]): Expr [Codec [T ]] = {
23
+ import q .reflect .*
24
+
25
+ val givenSelector : Selector = GivenSelector (None )
26
+ val omitSelector : Selector = OmitSelector (" named" )
27
+ val theImport = Import (deps.asTerm, List (givenSelector, omitSelector))
28
+ Block (List (theImport), ' {scala.compiletime.summonInline[Codec [T ]]}.asTerm).asExprOf[Codec [T ]]
29
+ /* import deps.{given, named => _}
30
+ * summonInline[Codec[T]]
31
+ */
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ //> using options -experimental
2
+
3
+ import scala .quoted .*
4
+
5
+ sealed trait Foo
6
+ class Bar extends Foo
7
+ object CustomCodecs {
8
+ given named : Codec [Bar ] = new Codec [Bar ] { def print (): Unit = println(" bar codec" )}
9
+ given Codec [Foo ] = new Codec [Foo ] { def print (): Unit = println(" foo codec" ) }
10
+ }
11
+
12
+ @ main def Test =
13
+ Codec .derivedWithDeps[Bar ](CustomCodecs ).print()
14
+ Codec .derivedWithDepsWithNamedOmitted[Bar ](CustomCodecs ).print()
You can’t perform that action at this time.
0 commit comments