Skip to content

Commit

Permalink
Merge pull request #35 from joroKr21/summon-first
Browse files Browse the repository at this point in the history
Remove code duplication of summonFirst0
  • Loading branch information
joroKr21 authored Sep 26, 2021
2 parents 34fa0c4 + f73c3c4 commit 505d446
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions modules/deriving/src/main/scala/shapeless3/deriving/kinds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ object K0 {
case _ => EmptyTuple
}

inline def summonFirst[F[_], T, U]: F[U] = summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]

transparent inline def summonFirst0[T]: Any = inline erasedValue[T] match {
case _: (a *: b) => summonFrom {
case aa: `a` => aa
case _ => summonFirst0[b]
}
}
inline def summonFirst[F[_], T, U]: F[U] =
summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]

transparent inline def summonFirst0[T]: Any =
inline erasedValue[T] match
case _: (a *: b) => summonFrom {
case instance: `a` => instance
case _ => summonFirst0[b]
}

extension [T](gen: ProductGeneric[T])
inline def toRepr(o: T): gen.MirroredElemTypes = Tuple.fromProduct(o.asInstanceOf).asInstanceOf[gen.MirroredElemTypes]
Expand Down Expand Up @@ -184,14 +185,11 @@ object K1 {
case _ => EmptyTuple
}

inline def summonFirst[F[_[_]], T[_], U[_]]: F[U] = summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]
inline def summonFirst[F[_[_]], T[_], U[_]]: F[U] =
K0.summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]

transparent inline def summonFirst0[T]: Any = inline erasedValue[T] match {
case _: (a *: b) => summonFrom {
case aa: `a` => aa
case _ => summonFirst0[b]
}
}
transparent inline def summonFirst0[T]: Any =
K0.summonFirst0[T]

extension [T[_], A] (gen: ProductGeneric[T])
inline def toRepr(o: T[A]): gen.MirroredElemTypes[A] = Tuple.fromProduct(o.asInstanceOf).asInstanceOf[gen.MirroredElemTypes[A]]
Expand Down Expand Up @@ -298,14 +296,11 @@ object K11 {
case _ => EmptyTuple
}

inline def summonFirst[F[_[_[_]]], T[_[_]], U[_[_]]]: F[U] = summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]
inline def summonFirst[F[_[_[_]]], T[_[_]], U[_[_]]]: F[U] =
K0.summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]

transparent inline def summonFirst0[T]: Any = inline erasedValue[T] match {
case _: (a *: b) => summonFrom {
case aa: `a` => aa
case _ => summonFirst0[b]
}
}
transparent inline def summonFirst0[T]: Any =
K0.summonFirst0[T]

extension [T[_[_]], A[_]](gen: ProductGeneric[T])
inline def toRepr(o: T[A]): gen.MirroredElemTypes[A] = Tuple.fromProduct(o.asInstanceOf).asInstanceOf[gen.MirroredElemTypes[A]]
Expand Down Expand Up @@ -413,14 +408,11 @@ object K2 {
case _ => EmptyTuple
}

inline def summonFirst[F[_[_, _]], T[_, _], U[_, _]]: F[U] = summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]
inline def summonFirst[F[_[_, _]], T[_, _], U[_, _]]: F[U] =
K0.summonFirst0[LiftP[F, T]].asInstanceOf[F[U]]

transparent inline def summonFirst0[T]: Any = inline erasedValue[T] match {
case _: (a *: b) => summonFrom {
case aa: `a` => aa
case _ => summonFirst0[b]
}
}
transparent inline def summonFirst0[T]: Any =
K0.summonFirst0[T]

extension [T[_, _], A, B](gen: ProductGeneric[T]) inline def toRepr(o: T[A, B]): gen.MirroredElemTypes[A, B] = Tuple.fromProduct(o.asInstanceOf).asInstanceOf[gen.MirroredElemTypes[A, B]]
extension [T[_, _], A, B](gen: ProductGeneric[T]) inline def fromRepr(r: gen.MirroredElemTypes[A, B]): T[A, B] = gen.fromProduct(r.asInstanceOf).asInstanceOf[T[A, B]]
Expand Down

0 comments on commit 505d446

Please sign in to comment.