Skip to content

Commit

Permalink
Merge pull request #568 from stew/more-unapply
Browse files Browse the repository at this point in the history
Add new shapes to Unapply
  • Loading branch information
non committed Oct 15, 2015
2 parents f69b3e1 + 1e8a4ea commit 757ff73
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions core/src/main/scala/cats/Unapply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ sealed abstract class Unapply2Instances extends Unapply3Instances {
type A = B
}

// the type we will instantiate when we find a type class instance
// for a type in the shape F[_,_[_]] when we fix the left type
type Aux2LeftK[TC[_[_]], FA, F[_,_[_]], AA, BX[_]] = Unapply[TC, FA] {
type M[X] = F[X,BX]
type A = AA
}

// the type we will instantiate when we find a type class instance
// for a type in the shape F[_[_],_] when we fix the right type,
type Aux2RightK[TC[_[_]], MA, F[_[_],_], AX[_], B] = Unapply[TC, MA] {
type M[X] = F[AX,X]
type A = B
}


implicit def unapply2left[TC[_[_]], F[_,_], AA, B](implicit tc: TC[F[?,B]]): Aux2Left[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
type M[X] = F[X, B]
Expand All @@ -80,6 +94,23 @@ sealed abstract class Unapply2Instances extends Unapply3Instances {
def subst: F[AA, B] => M[A] = identity
}


implicit def unapply2leftK[TC[_[_]], F[_,_[_]], AA, B[_]](implicit tc: TC[F[?,B]]): Aux2LeftK[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
type M[X] = F[X, B]
type A = AA
def TC: TC[F[?, B]] = tc
def subst: F[AA, B] => M[A] = identity
}

implicit def unapply2rightK[TC[_[_]], F[_[_],_], AA[_], B](implicit tc: TC[F[AA,?]]): Aux2RightK[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
type M[X] = F[AA, X]
type A = B
def TC: TC[F[AA, ?]] = tc
def subst: F[AA, B] => M[A] = identity
}



// STEW: I'm not sure why these Nothing cases are needed and aren't
// just caught by the generic cases, I'd love for someone to figure
// that out and report back.
Expand Down
10 changes: 10 additions & 0 deletions tests/src/test/scala/cats/tests/UnapplyTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ class UnapplyTests extends CatsSuite {
val x = Traverse[List].traverseU(List(1,2,3))(Xor.right(_))
(x: String Xor List[Int]) should === (Xor.right(List(1,2,3)))
}

test("Unapply works for F[_[_],_] with the left fixed") {
val x: OptionT[List, Int] = OptionT(List(Option(1), Option(2)))
val y: OptionT[List, Int] = OptionT(List(Option(3), Option(4)))

val z: List[Option[(Int,Int)]] = (x |@| y).tupled.value

z should be (List(Option((1,3)), Option((1,4)),
Option((2,3)), Option((2,4))))
}
}

0 comments on commit 757ff73

Please sign in to comment.