@@ -5,19 +5,19 @@ trait Deriving {
5
5
import Deriving ._
6
6
7
7
/** A mirror of case with ordinal number `ordinal` and elements as given by `Product` */
8
- def mirror [ T ] (ordinal : Int , product : Product ): CaseMirror [ T ] =
9
- new CaseMirror (this , ordinal, product)
8
+ def mirror (ordinal : Int , product : Product ): Mirror =
9
+ new Mirror (this , ordinal, product)
10
10
11
11
/** A mirror with elements given as an array */
12
- def mirror [ T ] (ordinal : Int , elems : Array [AnyRef ]): CaseMirror [ T ] =
12
+ def mirror (ordinal : Int , elems : Array [AnyRef ]): Mirror =
13
13
mirror(ordinal, new ArrayProduct (elems))
14
14
15
15
/** A mirror with an initial empty array of `numElems` elements, to be filled in. */
16
- def mirror [ T ] (ordinal : Int , numElems : Int ): CaseMirror [ T ] =
16
+ def mirror (ordinal : Int , numElems : Int ): Mirror =
17
17
mirror(ordinal, new Array [AnyRef ](numElems))
18
18
19
19
/** A mirror of a case with no elements */
20
- def mirror [ T ] (ordinal : Int ): CaseMirror [ T ] =
20
+ def mirror (ordinal : Int ): Mirror =
21
21
mirror(ordinal, EmptyProduct )
22
22
23
23
/** The case and element labels of the described ADT as encoded strings. */
@@ -46,7 +46,7 @@ object Deriving {
46
46
* @param ordinal The ordinal value of the case in the list of the ADT's cases
47
47
* @param elems The elements of the case
48
48
*/
49
- class CaseMirror [ + T ] (val deriving : Deriving , val ordinal : Int , val elems : Product ) {
49
+ class Mirror (val deriving : Deriving , val ordinal : Int , val elems : Product ) {
50
50
51
51
/** The `n`'th element of this generic case */
52
52
def apply (n : Int ): Any = elems.productElement(n)
@@ -64,16 +64,18 @@ object Deriving {
64
64
abstract class Reflected [T ] {
65
65
66
66
/** The case mirror corresponding to ADT instance `x` */
67
- def reflect (x : T ): CaseMirror [ T ]
67
+ def reflect (x : T ): Mirror
68
68
69
69
/** The ADT instance corresponding to given `mirror` */
70
- def reify (mirror : CaseMirror [ T ] ): T
70
+ def reify (mirror : Mirror ): T
71
71
72
72
/** The companion object of the ADT */
73
73
def deriving : Deriving
74
74
}
75
75
76
- /** The shape of an ADT in a sum of products representation */
76
+ /** The shape of an ADT.
77
+ * This is eithe a product (`Case`) or a sum (`Cases`) of products.
78
+ */
77
79
enum Shape {
78
80
79
81
/** A sum with alternative types `Alts` */
@@ -84,7 +86,7 @@ object Deriving {
84
86
}
85
87
86
88
/** Every generic derivation starts with a typeclass instance of this type.
87
- * It informs that type `T` has shape `S` and also allows runtime reflection on `T`.
89
+ * It informs that type `T` has shape `S` and also implements runtime reflection on `T`.
88
90
*/
89
91
abstract class Shaped [T , S <: Shape ] extends Reflected [T ]
90
92
@@ -121,14 +123,14 @@ object Lst extends Deriving {
121
123
Shape .Case [Nil .type , Unit ]
122
124
)]
123
125
124
- val NilMirror = mirror[ Nil . type ] (1 )
126
+ val NilMirror = mirror(1 )
125
127
126
128
implicit def lstShape [T ]: Shaped [Lst [T ], Shape [T ]] = new {
127
- def reflect (xs : Lst [T ]): CaseMirror [ Lst [ T ]] = xs match {
128
- case xs : Cons [T ] => mirror[ Cons [ T ]] (0 , xs)
129
+ def reflect (xs : Lst [T ]): Mirror = xs match {
130
+ case xs : Cons [T ] => mirror(0 , xs)
129
131
case Nil => NilMirror
130
132
}
131
- def reify (c : CaseMirror [ Lst [ T ]] ): Lst [T ] = c.ordinal match {
133
+ def reify (c : Mirror ): Lst [T ] = c.ordinal match {
132
134
case 0 => Cons [T ](c(0 ).asInstanceOf , c(1 ).asInstanceOf )
133
135
case 1 => Nil
134
136
}
@@ -154,8 +156,8 @@ object Pair extends Deriving {
154
156
155
157
implicit def pairShape [T ]: Shaped [Pair [T ], Shape [T ]] = new {
156
158
def reflect (xy : Pair [T ]) =
157
- mirror[ Pair [ T ]] (0 , xy)
158
- def reify (c : CaseMirror [ Pair [ T ]] ): Pair [T ] =
159
+ mirror(0 , xy)
160
+ def reify (c : Mirror ): Pair [T ] =
159
161
Pair (c(0 ).asInstanceOf , c(1 ).asInstanceOf )
160
162
def deriving = Pair
161
163
}
@@ -180,7 +182,7 @@ object Eq {
180
182
case eq : Eq [T ] => eq.eql(x, y)
181
183
}
182
184
183
- inline def eqlElems [Elems <: Tuple ](xs : CaseMirror [_] , ys : CaseMirror [_] , n : Int ): Boolean =
185
+ inline def eqlElems [Elems <: Tuple ](xs : Mirror , ys : Mirror , n : Int ): Boolean =
184
186
inline erasedValue[Elems ] match {
185
187
case _ : (elem *: elems1) =>
186
188
tryEql[elem](xs(n).asInstanceOf , ys(n).asInstanceOf ) &&
@@ -237,7 +239,7 @@ object Pickler {
237
239
case pkl : Pickler [T ] => pkl.pickle(buf, x)
238
240
}
239
241
240
- inline def pickleElems [Elems <: Tuple ](buf : mutable.ListBuffer [Int ], elems : CaseMirror [_] , n : Int ): Unit =
242
+ inline def pickleElems [Elems <: Tuple ](buf : mutable.ListBuffer [Int ], elems : Mirror , n : Int ): Unit =
241
243
inline erasedValue[Elems ] match {
242
244
case _ : (elem *: elems1) =>
243
245
tryPickle[elem](buf, elems(n).asInstanceOf [elem])
@@ -276,11 +278,11 @@ object Pickler {
276
278
inline def unpickleCase [T , Elems <: Tuple ](r : Reflected [T ], buf : mutable.ListBuffer [Int ], ordinal : Int ): T = {
277
279
inline val size = constValue[Tuple .Size [Elems ]]
278
280
inline if (size == 0 )
279
- r.reify(r.deriving.mirror[ T ] (ordinal))
281
+ r.reify(r.deriving.mirror(ordinal))
280
282
else {
281
283
val elems = new Array [Object ](size)
282
284
unpickleElems[Elems ](buf, elems, 0 )
283
- r.reify(r.deriving.mirror[ T ] (ordinal, elems))
285
+ r.reify(r.deriving.mirror(ordinal, elems))
284
286
}
285
287
}
286
288
@@ -326,7 +328,7 @@ object Show {
326
328
case s : Show [T ] => s.show(x)
327
329
}
328
330
329
- inline def showElems [Elems <: Tuple ](elems : CaseMirror [_] , n : Int ): List [String ] =
331
+ inline def showElems [Elems <: Tuple ](elems : Mirror , n : Int ): List [String ] =
330
332
inline erasedValue[Elems ] match {
331
333
case _ : (elem *: elems1) =>
332
334
val formal = elems.elementLabel(n)
0 commit comments