@@ -65,6 +65,7 @@ object Deriving {
6565 }
6666 }
6767}
68+ import Deriving ._
6869
6970// -- Example Datatypes ---------------------------------------------------------
7071
@@ -73,7 +74,6 @@ object Deriving {
7374sealed trait Lst [+ T ] // derives Eq, Pickler, Show
7475
7576object Lst {
76- import Deriving ._
7777
7878 class GenericLst [T ] extends Generic .Sum [Lst [T ]] {
7979 def ordinal (x : Lst [T ]) = x match {
@@ -92,17 +92,17 @@ object Lst {
9292
9393 case class Cons [T ](hd : T , tl : Lst [T ]) extends Lst [T ]
9494
95- object Cons {
95+ object Cons extends Generic . Product [ Cons [_]] {
9696 def apply [T ](x : T , xs : Lst [T ]): Lst [T ] = new Cons (x, xs)
9797
98- class GenericCons [T ] extends Generic .Product [Cons [T ]] {
98+ def fromProduct (p : Product ): Cons [_] =
99+ new Cons (productElement[Any ](p, 0 ), productElement[Lst [Any ]](p, 1 ))
100+
101+ implicit def GenericCons [T ]: Generic .Product [Cons [T ]] {
99102 type ElemTypes = (T , Lst [T ])
100103 type CaseLabel = " Cons"
101104 type ElemLabels = (" hd" , " tl" )
102- def fromProduct (p : Product ): Cons [T ] =
103- new Cons (productElement[T ](p, 0 ), productElement[Lst [T ]](p, 1 ))
104- }
105- implicit def GenericCons [T ]: GenericCons [T ] = new GenericCons [T ]
105+ } = this .asInstanceOf
106106 }
107107
108108 case object Nil extends Lst [Nothing ] with Generic .Singleton [Nil .type ] {
@@ -119,18 +119,16 @@ object Lst {
119119// A simple product type
120120case class Pair [T ](x : T , y : T ) // derives Eq, Pickler, Show
121121
122- object Pair {
123- // common compiler-generated infrastructure
124- import Deriving ._
122+ object Pair extends Generic .Product [Pair [_]] {
125123
126- class GenericPair [T ] extends Generic .Product [Pair [T ]] {
124+ def fromProduct (p : Product ): Pair [_] =
125+ Pair (productElement[Any ](p, 0 ), productElement[Any ](p, 1 ))
126+
127+ implicit def GenericPair [T ]: Generic .Product [Pair [T ]] {
127128 type ElemTypes = (T , T )
128129 type CaseLabel = " Pair"
129130 type ElemLabels = (" x" , " y" )
130- def fromProduct (p : Product ): Pair [T ] =
131- Pair (productElement[T ](p, 0 ), productElement[T ](p, 1 ))
132- }
133- implicit def GenericPair [T ]: GenericPair [T ] = new GenericPair [T ]
131+ } = this .asInstanceOf
134132
135133 // clauses that could be generated from a `derives` clause
136134 implicit def derived$Eq [T : Eq ]: Eq [Pair [T ]] = Eq .derived
@@ -142,12 +140,10 @@ object Pair {
142140sealed trait Either [+ L , + R ] extends Product with Serializable // derives Eq, Pickler, Show
143141
144142object Either {
145- import Deriving ._
146-
147143 class GenericEither [L , R ] extends Generic .Sum [Either [L , R ]] {
148144 def ordinal (x : Either [L , R ]) = x match {
149- case x : Left [L ] => 0
150- case x : Right [R ] => 1
145+ case x : Left [_ ] => 0
146+ case x : Right [_ ] => 1
151147 }
152148 inline override def numberOfCases = 2
153149 inline override def alternative (n : Int ) <: Generic [_ <: Either [L , R ]] =
@@ -166,26 +162,22 @@ object Either {
166162case class Left [L ](elem : L ) extends Either [L , Nothing ]
167163case class Right [R ](elem : R ) extends Either [Nothing , R ]
168164
169- object Left {
170- import Deriving . _
171- class GenericLeft [L ] extends Generic .Product [Left [L ]] {
165+ object Left extends Generic . Product [ Left [_]] {
166+ def fromProduct ( p : Product ) : Left [_] = Left (productElement[ Any ](p, 0 ))
167+ implicit def GenericLeft [L ]: Generic .Product [Left [L ]] {
172168 type ElemTypes = L *: Unit
173169 type CaseLabel = " Left"
174170 type ElemLabels = " x" *: Unit
175- def fromProduct (p : Product ): Left [L ] = Left (productElement[L ](p, 0 ))
176- }
177- implicit def GenericLeft [L ]: GenericLeft [L ] = new GenericLeft [L ]
171+ } = this .asInstanceOf
178172}
179173
180- object Right {
181- import Deriving . _
182- class GenericRight [R ] extends Generic .Product [Right [R ]] {
174+ object Right extends Generic . Product [ Right [_]] {
175+ def fromProduct ( p : Product ) : Right [_] = Right (productElement[ Any ](p, 0 ))
176+ implicit def GenericRight [R ]: Generic .Product [Right [R ]] {
183177 type ElemTypes = R *: Unit
184178 type CaseLabel = " Right"
185179 type ElemLabels = " x" *: Unit
186- def fromProduct (p : Product ): Right [R ] = Right (productElement[R ](p, 0 ))
187- }
188- implicit def GenericRight [R ]: GenericRight [R ] = new GenericRight [R ]
180+ } = this .asInstanceOf
189181}
190182
191183// -- Type classes ------------------------------------------------------------
@@ -199,7 +191,6 @@ trait Eq[T] {
199191
200192object Eq {
201193 import scala .compiletime .erasedValue
202- import Deriving ._
203194
204195 inline def tryEql [T ](x : T , y : T ) = implicit match {
205196 case eq : Eq [T ] => eq.eql(x, y)
@@ -253,7 +244,6 @@ trait Pickler[T] {
253244
254245object Pickler {
255246 import scala .compiletime .{erasedValue , constValue }
256- import Deriving ._
257247
258248 def nextInt (buf : mutable.ListBuffer [Int ]): Int = try buf.head finally buf.trimStart(1 )
259249
@@ -349,7 +339,6 @@ trait Show[T] {
349339}
350340object Show {
351341 import scala .compiletime .{erasedValue , constValue }
352- import Deriving ._
353342
354343 inline def tryShow [T ](x : T ): String = implicit match {
355344 case s : Show [T ] => s.show(x)
@@ -403,7 +392,6 @@ object Show {
403392// -- Tests ----------------------------------------------------------------------
404393
405394object Test extends App {
406- import Deriving ._
407395 val eq = implicitly[Eq [Lst [Int ]]]
408396 val xs = Lst .Cons (11 , Lst .Cons (22 , Lst .Cons (33 , Lst .Nil )))
409397 val ys = Lst .Cons (11 , Lst .Cons (22 , Lst .Nil ))
0 commit comments