@@ -65,6 +65,7 @@ object Deriving {
65
65
}
66
66
}
67
67
}
68
+ import Deriving ._
68
69
69
70
// -- Example Datatypes ---------------------------------------------------------
70
71
@@ -73,7 +74,6 @@ object Deriving {
73
74
sealed trait Lst [+ T ] // derives Eq, Pickler, Show
74
75
75
76
object Lst {
76
- import Deriving ._
77
77
78
78
class GenericLst [T ] extends Generic .Sum [Lst [T ]] {
79
79
def ordinal (x : Lst [T ]) = x match {
@@ -92,17 +92,17 @@ object Lst {
92
92
93
93
case class Cons [T ](hd : T , tl : Lst [T ]) extends Lst [T ]
94
94
95
- object Cons {
95
+ object Cons extends Generic . Product [ Cons [_]] {
96
96
def apply [T ](x : T , xs : Lst [T ]): Lst [T ] = new Cons (x, xs)
97
97
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 ]] {
99
102
type ElemTypes = (T , Lst [T ])
100
103
type CaseLabel = " Cons"
101
104
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
106
106
}
107
107
108
108
case object Nil extends Lst [Nothing ] with Generic .Singleton [Nil .type ] {
@@ -119,18 +119,16 @@ object Lst {
119
119
// A simple product type
120
120
case class Pair [T ](x : T , y : T ) // derives Eq, Pickler, Show
121
121
122
- object Pair {
123
- // common compiler-generated infrastructure
124
- import Deriving ._
122
+ object Pair extends Generic .Product [Pair [_]] {
125
123
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 ]] {
127
128
type ElemTypes = (T , T )
128
129
type CaseLabel = " Pair"
129
130
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
134
132
135
133
// clauses that could be generated from a `derives` clause
136
134
implicit def derived$Eq [T : Eq ]: Eq [Pair [T ]] = Eq .derived
@@ -142,12 +140,10 @@ object Pair {
142
140
sealed trait Either [+ L , + R ] extends Product with Serializable // derives Eq, Pickler, Show
143
141
144
142
object Either {
145
- import Deriving ._
146
-
147
143
class GenericEither [L , R ] extends Generic .Sum [Either [L , R ]] {
148
144
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
151
147
}
152
148
inline override def numberOfCases = 2
153
149
inline override def alternative (n : Int ) <: Generic [_ <: Either [L , R ]] =
@@ -166,26 +162,22 @@ object Either {
166
162
case class Left [L ](elem : L ) extends Either [L , Nothing ]
167
163
case class Right [R ](elem : R ) extends Either [Nothing , R ]
168
164
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 ]] {
172
168
type ElemTypes = L *: Unit
173
169
type CaseLabel = " Left"
174
170
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
178
172
}
179
173
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 ]] {
183
177
type ElemTypes = R *: Unit
184
178
type CaseLabel = " Right"
185
179
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
189
181
}
190
182
191
183
// -- Type classes ------------------------------------------------------------
@@ -199,7 +191,6 @@ trait Eq[T] {
199
191
200
192
object Eq {
201
193
import scala .compiletime .erasedValue
202
- import Deriving ._
203
194
204
195
inline def tryEql [T ](x : T , y : T ) = implicit match {
205
196
case eq : Eq [T ] => eq.eql(x, y)
@@ -253,7 +244,6 @@ trait Pickler[T] {
253
244
254
245
object Pickler {
255
246
import scala .compiletime .{erasedValue , constValue }
256
- import Deriving ._
257
247
258
248
def nextInt (buf : mutable.ListBuffer [Int ]): Int = try buf.head finally buf.trimStart(1 )
259
249
@@ -349,7 +339,6 @@ trait Show[T] {
349
339
}
350
340
object Show {
351
341
import scala .compiletime .{erasedValue , constValue }
352
- import Deriving ._
353
342
354
343
inline def tryShow [T ](x : T ): String = implicit match {
355
344
case s : Show [T ] => s.show(x)
@@ -403,7 +392,6 @@ object Show {
403
392
// -- Tests ----------------------------------------------------------------------
404
393
405
394
object Test extends App {
406
- import Deriving ._
407
395
val eq = implicitly[Eq [Lst [Int ]]]
408
396
val xs = Lst .Cons (11 , Lst .Cons (22 , Lst .Cons (33 , Lst .Nil )))
409
397
val ys = Lst .Cons (11 , Lst .Cons (22 , Lst .Nil ))
0 commit comments