Permalink
Browse files

Add IsHCons.cons & IsCCons.cons

  • Loading branch information...
1 parent d10889a commit 7a1b7ee3004985d8d25611eab84f0b8aea457f8e @dwijnand dwijnand committed Dec 11, 2016
View
@@ -275,7 +275,9 @@ lazy val mimaSettings = mimaDefaultSettings ++ Seq(
exclude[MissingMethodProblem]("shapeless.UnwrappedInstances.tagUnwrapped"),
exclude[MissingMethodProblem]("shapeless.CaseClassMacros.findField"),
exclude[MissingMethodProblem]("shapeless.CaseClassMacros.FieldType"),
- exclude[MissingMethodProblem]("shapeless.SingletonTypeUtils.parseSingletonSymbolType")
+ exclude[MissingMethodProblem]("shapeless.SingletonTypeUtils.parseSingletonSymbolType"),
+ exclude[MissingMethodProblem]("shapeless.ops.hlist#IsHCons.cons"),
+ exclude[MissingMethodProblem]("shapeless.ops.coproduct#IsCCons.cons")
)
}
)
@@ -859,6 +859,7 @@ object coproduct {
def head(c: C): Option[H]
def tail(c: C): Option[T]
+ def cons(e: Either[H, T]): C
}
object IsCCons {
@@ -879,6 +880,11 @@ object coproduct {
case Inr(t) => Some(t)
case _ => None
}
+
+ def cons(e: Either[H0, T0]): H0 :+: T0 = e match {
+ case Left(h) => Inl(h)
+ case Right(t) => Inr(t)
+ }
}
}
/**
@@ -38,6 +38,7 @@ object hlist {
def head(l : L) : H
def tail(l : L) : T
+ def cons(h : H, t : T) : L
}
object IsHCons {
@@ -51,6 +52,7 @@ object hlist {
def head(l : H0 :: T0) : H = l.head
def tail(l : H0 :: T0) : T = l.tail
+ def cons(h : H0, t : T0) : H0 :: T0 = h :: t
}
}
@@ -1882,6 +1882,12 @@ class CoproductTests {
assertEquals(FInt :: HNil, LiftAll[F](Coproduct[Int :+: CNil](1)).instances)
}
+ @Test
+ def testIsCCons = {
+ val isCCons = IsCCons[Int :+: String :+: CNil]
+ assertEquals(Inl(23), isCCons.cons(Left(23)))
+ assertEquals(Inr(Inl("bar")), isCCons.cons(Right(Inl("bar"))))
+ }
}
package CoproductTestAux {
@@ -3342,4 +3342,7 @@ class HListTests {
val r5 = (1 :: "2" :: 3 :: 4 :: HNil).combinations(0)
assertTypedEquals[HNil :: HNil](HNil :: HNil, r5)
}
+
+ @Test
+ def testIsHCons = assertTypedEquals[Int :: HNil](23 :: HNil, IsHCons[Int :: HNil].cons(23, HNil))
}

0 comments on commit 7a1b7ee

Please sign in to comment.