Skip to content

Instantly share code, notes, and snippets.

@jcoveney
Created September 17, 2014 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jcoveney/651fef547b8e9087547f to your computer and use it in GitHub Desktop.
Save jcoveney/651fef547b8e9087547f to your computer and use it in GitHub Desktop.
Not sure why this is diverging...
// in bijection branch jco/macro_case_class_plus
// ./sbt "bijection-macros/console"
// paste the following
import com.twitter.bijection._
import com.twitter.bijection.macros.common.TypesNotEqual
import com.twitter.bijection.macros.common.MacroImplicits._
trait Semigroup[T] {
def plus(left: T, right: T): T
}
implicit val intSemigroup: Semigroup[Int] = new Semigroup[Int] {
override def plus(left: Int, right: Int) = left + right
}
case class Wrapper(get: Int) extends AnyVal
implicit val intWrapperBij: Bijection[Int, Wrapper] = Bijection.build[Int, Wrapper] { Wrapper(_) } { _.get }
trait TypeclassBijection[T[_]] {
def apply[A, B](tc: T[A], bij: Bijection[A, B]): T[B]
}
implicit val semigroupTypeclassBijection: TypeclassBijection[Semigroup] = new TypeclassBijection[Semigroup] {
def apply[A, B](tc: Semigroup[A], bij: Bijection[A, B]) =
new Semigroup[B] {
override def plus(left: B, right: B) = bij(tc.plus(bij.invert(left), bij.invert(right)))
}
}
implicit def typeclassBijection[T[_], A, B](implicit tcBij: TypeclassBijection[T], typeclass: T[A], notEqual: TypesNotEqual[A, B], bij: ImplicitBijection[A, B]): T[B] = tcBij(typeclass, bij.bijection)
implicitly[Semigroup[Wrapper]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment