Skip to content

Commit

Permalink
Change nested pairs to HList conversions for consistency
Browse files Browse the repository at this point in the history
To be more consistent with nested `Either` to `Coproduct` conversions.
  • Loading branch information
joroKr21 committed Mar 18, 2020
1 parent 506e397 commit 3ca0948
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
42 changes: 21 additions & 21 deletions core/src/main/scala/shapeless/ops/hlists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3074,26 +3074,26 @@ object hlist {
*
* @author Michael Zuber
*/
sealed trait ToHList[-T] {
sealed trait ProductToHList[-T] extends Serializable {
type Out <: HList
def apply(t: T): Out
}

object ToHList {
def apply[P](implicit ev: ToHList[P]): ToHList[P] = ev
object ProductToHList {
def apply[P](implicit ev: ProductToHList[P]): ProductToHList[P] = ev

type Aux[P, HL <: HList] = ToHList[P] { type Out = HL }
type Aux[P, HL <: HList] = ProductToHList[P] { type Out = HL }

implicit def hconsToHList[H, TH, TT, HL <: HList](implicit
ev: ToHList.Aux[Product2[TH, TT], HL]
): ToHList.Aux[Product2[H, Product2[TH, TT]], H :: HL] = new ToHList[Product2[H, Product2[TH, TT]]] {
implicit def pairToHCons[H, T, HL <: HList](
implicit ev: ProductToHList.Aux[T, HL]
): ProductToHList.Aux[Product2[H, T], H :: HL] = new ProductToHList[Product2[H, T]] {
type Out = H :: HL
def apply(p: Product2[H, Product2[TH, TT]]): Out = p._1 :: ev(p._2)
def apply(p: Product2[H, T]): Out = p._1 :: ev(p._2)
}

implicit def hnilToHList[H]: ToHList.Aux[Product2[H, Unit], H :: HNil] = new ToHList[Product2[H, Unit]] {
type Out = H :: HNil
def apply(p: Product2[H, Unit]): Out = p._1 :: HNil
implicit val unitToHNil: ProductToHList.Aux[Unit, HNil] = new ProductToHList[Unit] {
type Out = HNil
def apply(p: Unit): Out = HNil
}
}

Expand All @@ -3102,23 +3102,23 @@ object hlist {
*
* @author Michael Zuber
*/
sealed trait HListToProduct[HL <: HList] extends DepFn1[HL] { type Out <: Product }
sealed trait HListToProduct[HL <: HList] extends DepFn1[HL] with Serializable

object HListToProduct {
def apply[HL <: HList](implicit ev: HListToProduct[HL]): HListToProduct[HL] = ev

type Aux[HL <: HList, P <: Product] = HListToProduct[HL] { type Out = P }
type Aux[HL <: HList, P] = HListToProduct[HL] { type Out = P }

implicit def hnilToProduct[H]: HListToProduct.Aux[H :: HNil, Product2[H, Unit]] = new HListToProduct[H :: HNil] {
type Out = Product2[H, Unit]
def apply(hl: H :: HNil): Out = (hl.head, ())
implicit val hnilToUnit: HListToProduct.Aux[HNil, Unit] = new HListToProduct[HNil] {
type Out = Unit
def apply(hl: HNil): Out = ()
}

implicit def hconsHListToProduct[H, T <: HList, TP <: Product](implicit
ev: HListToProduct.Aux[T, TP]
): HListToProduct.Aux[H :: T, Product2[H, TP]] = new HListToProduct[H :: T] {
type Out = Product2[H, TP]
def apply(hl: H :: T) = (hl.head, ev(hl.tail))
implicit def hconsToPair[H, T <: HList, TP](
implicit ev: HListToProduct.Aux[T, TP]
): HListToProduct.Aux[H :: T, (H, TP)] = new HListToProduct[H :: T] {
type Out = (H, TP)
def apply(hl: H :: T): Out = (hl.head, ev(hl.tail))
}
}
}
2 changes: 1 addition & 1 deletion core/src/main/scala/shapeless/syntax/hlists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -748,5 +748,5 @@ final class HListOps[L <: HList](l : L) extends Serializable {
/**
* Converts this `HList` into a nested pair
*/
def nestedProduct(implicit hListToProduct: HListToProduct[L]): hListToProduct.Out = hListToProduct(l)
def toProduct(implicit hListToProduct: HListToProduct[L]): hListToProduct.Out = hListToProduct(l)
}
4 changes: 3 additions & 1 deletion core/src/main/scala/shapeless/syntax/std/tuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package shapeless
package syntax
package std

import shapeless.ops.hlist.ProductToHList

trait LowPriorityTuple {
implicit def productTupleOps[P <: Product](p: P): TupleOps[P] = new TupleOps(p)
}
Expand Down Expand Up @@ -524,5 +526,5 @@ final class TupleOps[T](t: T) extends Serializable {
/**
* Converts this nested `Product` into an `HList`
*/
def toHList(implicit toHList: ops.hlist.ToHList[T]): toHList.Out = toHList(t)
def toHList(implicit productToHList: ProductToHList[T]): productToHList.Out = productToHList(t)
}
8 changes: 5 additions & 3 deletions core/src/test/scala/shapeless/hlist.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3490,9 +3490,11 @@ class HListTests {
val p = (2, ("abc", (true, (3.0, ()))))

import syntax.std.tuple._
assertEquals(isbd.nestedProduct, p)
assertEquals(isbd.toProduct, p)
assertEquals(p.toHList, isbd)
assertEquals(isbd.nestedProduct.toHList, isbd)
assertEquals(p.toHList.nestedProduct, p)
assertEquals(isbd.toProduct.toHList, isbd)
assertEquals(p.toHList.toProduct, p)
assertEquals((), (HNil: HNil).toProduct)
assertEquals(HNil, ().toHList)
}
}

0 comments on commit 3ca0948

Please # to comment.