@@ -17,7 +17,7 @@ object Deriving {
17
17
* @param ordinal The ordinal value of the case in the list of the ADT's cases
18
18
* @param elems The elements of the case
19
19
*/
20
- class GenericCase [+ T ](val ordinal : Int , val elems : Product ) {
20
+ class CaseMirror [+ T ](val ordinal : Int , val elems : Product ) {
21
21
22
22
/** A generic case with elements given as an array */
23
23
def this (ordinal : Int , elems : Array [AnyRef ]) =
@@ -54,15 +54,15 @@ object Deriving {
54
54
/** A class for mapping between an ADT value and
55
55
* the generic case that represents the value
56
56
*/
57
- abstract class GenericMapper [T ] {
58
- def toGenericCase (x : T ): GenericCase [T ]
59
- def fromGenericCase (c : GenericCase [T ]): T
57
+ abstract class MirrorMapper [T ] {
58
+ def toMirror (x : T ): CaseMirror [T ]
59
+ def fromMirror (c : CaseMirror [T ]): T
60
60
}
61
61
62
62
/** Every generic derivation starts with a typeclass instance of this type.
63
63
* It informs that type `T` has shape `S` and is backed by the extended generic mapper.
64
64
*/
65
- abstract class HasShape [T , S <: Shape ] extends GenericMapper [T ]
65
+ abstract class Shaped [T , S <: Shape ] extends MirrorMapper [T ]
66
66
}
67
67
68
68
// An algebraic datatype
@@ -76,19 +76,19 @@ object Lst {
76
76
// common compiler-generated infrastructure
77
77
import Deriving ._
78
78
79
- type LstShape [T ] = Shape .Cases [(
79
+ type Shape [T ] = Shape .Cases [(
80
80
Shape .Case [Cons [T ], (T , Lst [T ])],
81
81
Shape .Case [Nil .type , Unit ]
82
82
)]
83
83
84
- val NilCase = new GenericCase [Nil .type ](1 )
84
+ val NilMirror = new CaseMirror [Nil .type ](1 )
85
85
86
- implicit def lstShape [T ]: HasShape [Lst [T ], LstShape [T ]] = new {
87
- def toGenericCase (xs : Lst [T ]): GenericCase [Lst [T ]] = xs match {
88
- case xs : Cons [T ] => new GenericCase [Cons [T ]](0 , xs)
89
- case Nil => NilCase
86
+ implicit def lstShape [T ]: Shaped [Lst [T ], Shape [T ]] = new {
87
+ def toMirror (xs : Lst [T ]): CaseMirror [Lst [T ]] = xs match {
88
+ case xs : Cons [T ] => new CaseMirror [Cons [T ]](0 , xs)
89
+ case Nil => NilMirror
90
90
}
91
- def fromGenericCase (c : GenericCase [Lst [T ]]): Lst [T ] = c.ordinal match {
91
+ def fromMirror (c : CaseMirror [Lst [T ]]): Lst [T ] = c.ordinal match {
92
92
case 0 => Cons [T ](c(0 ).asInstanceOf , c(1 ).asInstanceOf )
93
93
case 1 => Nil
94
94
}
@@ -106,12 +106,12 @@ object Pair {
106
106
// common compiler-generated infrastructure
107
107
import Deriving ._
108
108
109
- type PairShape [T ] = Shape .Case [Pair [T ], (T , T )]
109
+ type Shape [T ] = Shape .Case [Pair [T ], (T , T )]
110
110
111
- implicit def pairShape [T ]: HasShape [Pair [T ], PairShape [T ]] = new {
112
- def toGenericCase (xy : Pair [T ]) =
113
- new GenericCase [Pair [T ]](0 , xy)
114
- def fromGenericCase (c : GenericCase [Pair [T ]]): Pair [T ] =
111
+ implicit def pairShape [T ]: Shaped [Pair [T ], Shape [T ]] = new {
112
+ def toMirror (xy : Pair [T ]) =
113
+ new CaseMirror [Pair [T ]](0 , xy)
114
+ def fromMirror (c : CaseMirror [Pair [T ]]): Pair [T ] =
115
115
Pair (c(0 ).asInstanceOf , c(1 ).asInstanceOf )
116
116
}
117
117
@@ -133,7 +133,7 @@ object Eq {
133
133
case eq : Eq [T ] => eq.eql(x, y)
134
134
}
135
135
136
- inline def eqlElems [Elems <: Tuple ](xs : GenericCase [_], ys : GenericCase [_], n : Int ): Boolean =
136
+ inline def eqlElems [Elems <: Tuple ](xs : CaseMirror [_], ys : CaseMirror [_], n : Int ): Boolean =
137
137
inline erasedValue[Elems ] match {
138
138
case _ : (elem *: elems1) =>
139
139
tryEql[elem](xs(n).asInstanceOf , ys(n).asInstanceOf ) &&
@@ -142,10 +142,10 @@ object Eq {
142
142
true
143
143
}
144
144
145
- inline def eqlCase [T , Elems <: Tuple ](mapper : GenericMapper [T ], x : T , y : T ) =
146
- eqlElems[Elems ](mapper.toGenericCase (x), mapper.toGenericCase (y), 0 )
145
+ inline def eqlCase [T , Elems <: Tuple ](mapper : MirrorMapper [T ], x : T , y : T ) =
146
+ eqlElems[Elems ](mapper.toMirror (x), mapper.toMirror (y), 0 )
147
147
148
- inline def eqlCases [T , Alts <: Tuple ](mapper : GenericMapper [T ], x : T , y : T ): Boolean =
148
+ inline def eqlCases [T , Alts <: Tuple ](mapper : MirrorMapper [T ], x : T , y : T ): Boolean =
149
149
inline erasedValue[Alts ] match {
150
150
case _ : (Shape .Case [alt, elems] *: alts1) =>
151
151
x match {
@@ -160,7 +160,7 @@ object Eq {
160
160
false
161
161
}
162
162
163
- inline def derived [T , S <: Shape ](implicit ev : HasShape [T , S ]) <: Eq [T ] = new {
163
+ inline def derived [T , S <: Shape ](implicit ev : Shaped [T , S ]) <: Eq [T ] = new {
164
164
def eql (x : T , y : T ): Boolean = inline erasedValue[S ] match {
165
165
case _ : Shape .Cases [alts] =>
166
166
eqlCases[T , alts](ev, x, y)
@@ -190,18 +190,18 @@ object Pickler {
190
190
case pkl : Pickler [T ] => pkl.pickle(buf, x)
191
191
}
192
192
193
- inline def pickleElems [Elems <: Tuple ](buf : mutable.ListBuffer [Int ], elems : GenericCase [_], n : Int ): Unit =
193
+ inline def pickleElems [Elems <: Tuple ](buf : mutable.ListBuffer [Int ], elems : CaseMirror [_], n : Int ): Unit =
194
194
inline erasedValue[Elems ] match {
195
195
case _ : (elem *: elems1) =>
196
196
tryPickle[elem](buf, elems(n).asInstanceOf [elem])
197
197
pickleElems[elems1](buf, elems, n + 1 )
198
198
case _ : Unit =>
199
199
}
200
200
201
- inline def pickleCase [T , Elems <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], x : T ): Unit =
202
- pickleElems[Elems ](buf, mapper.toGenericCase (x), 0 )
201
+ inline def pickleCase [T , Elems <: Tuple ](mapper : MirrorMapper [T ], buf : mutable.ListBuffer [Int ], x : T ): Unit =
202
+ pickleElems[Elems ](buf, mapper.toMirror (x), 0 )
203
203
204
- inline def pickleCases [T , Alts <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], x : T , n : Int ): Unit =
204
+ inline def pickleCases [T , Alts <: Tuple ](mapper : MirrorMapper [T ], buf : mutable.ListBuffer [Int ], x : T , n : Int ): Unit =
205
205
inline erasedValue[Alts ] match {
206
206
case _ : (Shape .Case [alt, elems] *: alts1) =>
207
207
x match {
@@ -226,18 +226,18 @@ object Pickler {
226
226
case _ : Unit =>
227
227
}
228
228
229
- inline def unpickleCase [T , Elems <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], ordinal : Int ): T = {
229
+ inline def unpickleCase [T , Elems <: Tuple ](mapper : MirrorMapper [T ], buf : mutable.ListBuffer [Int ], ordinal : Int ): T = {
230
230
inline val size = constValue[Tuple .Size [Elems ]]
231
231
inline if (size == 0 )
232
- mapper.fromGenericCase (new GenericCase [T ](ordinal))
232
+ mapper.fromMirror (new CaseMirror [T ](ordinal))
233
233
else {
234
234
val elems = new Array [Object ](size)
235
235
unpickleElems[Elems ](buf, elems, 0 )
236
- mapper.fromGenericCase (new GenericCase [T ](ordinal, elems))
236
+ mapper.fromMirror (new CaseMirror [T ](ordinal, elems))
237
237
}
238
238
}
239
239
240
- inline def unpickleCases [T , Alts <: Tuple ](mapper : GenericMapper [T ], buf : mutable.ListBuffer [Int ], ordinal : Int , n : Int ): T =
240
+ inline def unpickleCases [T , Alts <: Tuple ](mapper : MirrorMapper [T ], buf : mutable.ListBuffer [Int ], ordinal : Int , n : Int ): T =
241
241
inline erasedValue[Alts ] match {
242
242
case _ : (Shape .Case [_, elems] *: alts1) =>
243
243
if (n == ordinal) unpickleCase[T , elems](mapper, buf, ordinal)
@@ -246,7 +246,7 @@ object Pickler {
246
246
throw new IndexOutOfBoundsException (s " unexpected ordinal number: $ordinal" )
247
247
}
248
248
249
- inline def derived [T , S <: Shape ](implicit ev : HasShape [T , S ]): Pickler [T ] = new {
249
+ inline def derived [T , S <: Shape ](implicit ev : Shaped [T , S ]): Pickler [T ] = new {
250
250
def pickle (buf : mutable.ListBuffer [Int ], x : T ): Unit = inline erasedValue[S ] match {
251
251
case _ : Shape .Cases [alts] =>
252
252
pickleCases[T , alts](ev, buf, x, 0 )
0 commit comments