Skip to content

Commit

Permalink
Make RichTypeclass implicit class
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Coveney committed Sep 17, 2014
1 parent 3b408e9 commit d377d8a
Showing 1 changed file with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.twitter.bijection

object TypeclassBijection {
implicit def toRichTypeclass[T[_], A](t: T[A]): RichTypeclass[T, A] = RichTypeclass(t)
def typeclassBijection[T[_], A, B](implicit tcBij: TypeclassBijection[T], typeclass: T[A], bij: ImplicitBijection[A, B]): T[B] = tcBij(typeclass, bij.bijection)
implicit class RichTypeclass[T[_], A](t: T[A]) {
def bijectTo[B](implicit tcBij: TypeclassBijection[T], bij: ImplicitBijection[A, B]): T[B] = tcBij(t, bij.bijection)
}

object BijectionAndTypeclass {
implicit def get[T[_], From, To](implicit bij: ImplicitBijection[To, From], typeclass: T[From]) = BijectionAndTypeclass(bij, typeclass)
Expand All @@ -11,12 +12,9 @@ object TypeclassBijection {
def apply(tc: TypeclassBijection[T]): T[To] = TypeclassBijection.typeclassBijection[T, From, To](tc, typeclass, ImplicitBijection.reverse(bij.bijection))
}

def typeclassBijection[T[_], A, B](implicit tcBij: TypeclassBijection[T], typeclass: T[A], bij: ImplicitBijection[A, B]): T[B] = tcBij(typeclass, bij.bijection)
def deriveFor[T[_], To](implicit tcBij: TypeclassBijection[T], batc: BijectionAndTypeclass[T, From, To] forSome { type From }): T[To] = batc(tcBij)
}
trait TypeclassBijection[T[_]] {
def apply[A, B](tc: T[A], bij: Bijection[A, B]): T[B]
}

case class RichTypeclass[T[_], A](t: T[A]) {
def bijectTo[B](implicit tcBij: TypeclassBijection[T], bij: ImplicitBijection[A, B]): T[B] = tcBij(t, bij.bijection)
}

0 comments on commit d377d8a

Please sign in to comment.