Skip to content

Commit 2243e2d

Browse files
committed
Eliminate toProduct in test case
1 parent 20e54a3 commit 2243e2d

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

tests/run/typeclass-derivation2c.scala

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ object Deriving {
2323
def update(n: Int, x: Any) = elems(n) = x.asInstanceOf[AnyRef]
2424
}
2525

26+
def productElement[T](x: Any, idx: Int) = x.asInstanceOf[Product].productElement(idx).asInstanceOf[T]
27+
2628
/** The Generic class hierarchy allows typelevel access to
2729
* enums, case classes and objects, and their sealed parents.
2830
*/
@@ -52,7 +54,6 @@ object Deriving {
5254
type CaseLabel <: String
5355
type ElemLabels <: Tuple
5456

55-
def toProduct(x: T): scala.Product
5657
def fromProduct(p: scala.Product): T
5758
}
5859

@@ -95,10 +96,8 @@ object Lst {
9596
type ElemTypes = (T, Lst[T])
9697
type CaseLabel = "Cons"
9798
type ElemLabels = ("hd", "tl")
98-
def toProduct(x: Cons[T]): Product = x
9999
def fromProduct(p: Product): Cons[T] =
100-
new Cons(p.productElement(0).asInstanceOf[T],
101-
p.productElement(1).asInstanceOf[Lst[T]])
100+
new Cons(productElement[T](p, 0), productElement[Lst[T]](p, 1))
102101
}
103102
implicit def GenericCons[T]: GenericCons[T] = new GenericCons[T]
104103
}
@@ -127,9 +126,8 @@ object Pair {
127126
type ElemTypes = (T, T)
128127
type CaseLabel = "Pair"
129128
type ElemLabels = ("x", "y")
130-
def toProduct(x: Pair[T]): Product = x
131129
def fromProduct(p: Product): Pair[T] =
132-
Pair(p.productElement(0).asInstanceOf, p.productElement(1).asInstanceOf)
130+
Pair(productElement[T](p, 0), productElement[T](p, 1))
133131
}
134132
implicit def GenericPair[T]: GenericPair[T] = new GenericPair[T]
135133

@@ -173,8 +171,7 @@ object Left {
173171
type ElemTypes = L *: Unit
174172
type CaseLabel = "Left"
175173
type ElemLabels = "x" *: Unit
176-
def toProduct(x: Left[L]) = x
177-
def fromProduct(p: Product): Left[L] = Left(p.productElement(0).asInstanceOf[L])
174+
def fromProduct(p: Product): Left[L] = Left(productElement[L](p, 0))
178175
}
179176
implicit def GenericLeft[L]: GenericLeft[L] = new GenericLeft[L]
180177
}
@@ -185,8 +182,7 @@ object Right {
185182
type ElemTypes = R *: Unit
186183
type CaseLabel = "Right"
187184
type ElemLabels = "x" *: Unit
188-
def toProduct(x: Right[R]) = x
189-
def fromProduct(p: Product): Right[R] = Right(p.productElement(0).asInstanceOf[R])
185+
def fromProduct(p: Product): Right[R] = Right(productElement[R](p, 0))
190186
}
191187
implicit def GenericRight[R]: GenericRight[R] = new GenericRight[R]
192188
}
@@ -208,26 +204,24 @@ object Eq {
208204
case eq: Eq[T] => eq.eql(x, y)
209205
}
210206

211-
inline def eqlElems[Elems <: Tuple](n: Int)(x: Product, y: Product): Boolean =
207+
inline def eqlElems[Elems <: Tuple](n: Int)(x: Any, y: Any): Boolean =
212208
inline erasedValue[Elems] match {
213209
case _: (elem *: elems1) =>
214-
tryEql[elem](
215-
x.productElement(n).asInstanceOf[elem],
216-
y.productElement(n).asInstanceOf[elem]) &&
210+
tryEql[elem](productElement[elem](x, n), productElement[elem](y, n)) &&
217211
eqlElems[elems1](n + 1)(x, y)
218212
case _: Unit =>
219213
true
220214
}
221215

222-
inline def eqlProduct[T](g: Generic.Product[T])(x: T, y: T): Boolean =
223-
eqlElems[g.ElemTypes](0)(g.toProduct(x), g.toProduct(y))
216+
inline def eqlProduct[T](g: Generic.Product[T])(x: Any, y: Any): Boolean =
217+
eqlElems[g.ElemTypes](0)(x, y)
224218

225219
inline def eqlCases[T](g: Generic.Sum[T], n: Int)(x: T, y: T, ord: Int): Boolean =
226220
inline if (n == g.numberOfCases)
227221
false
228222
else if (ord == n)
229223
inline g.alternative(n) match {
230-
case g: Generic.Product[p] => eqlProduct[p](g)(x.asInstanceOf[p], y.asInstanceOf[p])
224+
case g: Generic.Product[p] => eqlProduct[p](g)(x, y)
231225
case g: Generic.Singleton[_] => true
232226
}
233227
else eqlCases[T](g, n + 1)(x, y, ord)
@@ -266,23 +260,23 @@ object Pickler {
266260
case pkl: Pickler[T] => pkl.pickle(buf, x)
267261
}
268262

269-
inline def pickleElems[Elems <: Tuple](n: Int)(buf: mutable.ListBuffer[Int], x: Product): Unit =
263+
inline def pickleElems[Elems <: Tuple](n: Int)(buf: mutable.ListBuffer[Int], x: Any): Unit =
270264
inline erasedValue[Elems] match {
271265
case _: (elem *: elems1) =>
272-
tryPickle[elem](buf, x.productElement(n).asInstanceOf[elem])
266+
tryPickle[elem](buf, productElement[elem](x, n))
273267
pickleElems[elems1](n + 1)(buf, x)
274268
case _: Unit =>
275269
}
276270

277-
inline def pickleProduct[T](g: Generic.Product[T])(buf: mutable.ListBuffer[Int], x: T): Unit =
278-
pickleElems[g.ElemTypes](0)(buf, g.toProduct(x))
271+
inline def pickleProduct[T](g: Generic.Product[T])(buf: mutable.ListBuffer[Int], x: Any): Unit =
272+
pickleElems[g.ElemTypes](0)(buf, x)
279273

280274
inline def pickleCases[T](g: Generic.Sum[T], inline n: Int)(buf: mutable.ListBuffer[Int], x: T, ord: Int): Unit =
281275
inline if (n == g.numberOfCases)
282276
()
283277
else if (ord == n)
284278
inline g.alternative(n) match {
285-
case g: Generic.Product[p] => pickleProduct(g)(buf, x.asInstanceOf[p])
279+
case g: Generic.Product[p] => pickleProduct(g)(buf, x)
286280
case g: Generic.Singleton[s] =>
287281
}
288282
else pickleCases[T](g, n + 1)(buf, x, ord)
@@ -360,30 +354,30 @@ object Show {
360354
case s: Show[T] => s.show(x)
361355
}
362356

363-
inline def showElems[Elems <: Tuple, Labels <: Tuple](n: Int)(x: Product): List[String] =
357+
inline def showElems[Elems <: Tuple, Labels <: Tuple](n: Int)(x: Any): List[String] =
364358
inline erasedValue[Elems] match {
365359
case _: (elem *: elems1) =>
366360
inline erasedValue[Labels] match {
367361
case _: (label *: labels1) =>
368362
val formal = constValue[label]
369-
val actual = tryShow(x.productElement(n).asInstanceOf[elem])
363+
val actual = tryShow(productElement[elem](x, n))
370364
s"$formal = $actual" :: showElems[elems1, labels1](n + 1)(x)
371365
}
372366
case _: Unit =>
373367
Nil
374368
}
375369

376-
inline def showProduct[T](g: Generic.Product[T])(x: T): String = {
370+
inline def showProduct[T](g: Generic.Product[T])(x: Any): String = {
377371
val labl = constValue[g.CaseLabel]
378-
showElems[g.ElemTypes, g.ElemLabels](0)(g.toProduct(x)).mkString(s"$labl(", ", ", ")")
372+
showElems[g.ElemTypes, g.ElemLabels](0)(x).mkString(s"$labl(", ", ", ")")
379373
}
380374

381375
inline def showCases[T](g: Generic.Sum[T], n: Int)(x: T, ord: Int): String =
382376
inline if (n == g.numberOfCases)
383377
""
384378
else if (ord == n)
385379
inline g.alternative(n) match {
386-
case g: Generic.Product[p] => showProduct(g)(x.asInstanceOf[p])
380+
case g: Generic.Product[p] => showProduct(g)(x)
387381
case g: Generic.Singleton[s] => constValue[g.CaseLabel]
388382
}
389383
else showCases[T](g, n + 1)(x, ord)

0 commit comments

Comments
 (0)