Last active
June 23, 2016 19:20
-
-
Save shajra/f6c90d384f22667f47ba59084f12868d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Translate[A, B] { | |
| def apply(a: A): B | |
| } | |
| trait TranslateSyntax { | |
| implicit class TranslateOps[A](a: A) { | |
| def translate[B](implicit tr: Translate[A, B]): B = tr(a) | |
| } | |
| } | |
| object TranslateSyntax extends TranslateSyntax | |
| object Translate { | |
| @inline | |
| def low[A, B](f: A => B): Translate[A, B] with LowPrecedence = | |
| new Translate[A, B] with LowPrecedence { def apply(a: A) = f(a) } | |
| @inline | |
| def high[A, B](f: A => B): Translate[A, B] with HighPrecedence = | |
| new Translate[A, B] with HighPrecedence { def apply(a: A) = f(a) } | |
| implicit def transitive0[A, B, C](implicit tab: Translate[A, B] with HighPrecedence, tbc: Translate[B, C] with LowPrecedence): Translate[A, C] with LowPrecedence = | |
| Translate low { a => tbc(tab(a)) } | |
| implicit def transitive1[A, B, C](implicit tab: Translate[A, B] with HighPrecedence, tbc: Translate[B, C] with HighPrecedence): Translate[A, C] with LowPrecedence = | |
| Translate low { a => tbc(tab(a)) } | |
| } | |
| final case class AA(i: Int) | |
| object AA { | |
| implicit def translate: Translate[AA, BB] with HighPrecedence = | |
| Translate high { x => BB(x.i) } | |
| } | |
| final case class BB(i: Int) | |
| object BB { | |
| implicit def translate: Translate[BB, CC] with HighPrecedence = | |
| Translate high { x => CC(x.i) } | |
| } | |
| final case class CC(i: Int) | |
| object CC { | |
| implicit def translate: Translate[CC, DD] with HighPrecedence = | |
| Translate high { x => DD(x.i) } | |
| } | |
| final case class DD(i: Int) | |
| object DD { | |
| implicit def translate: Translate[DD, EE] with HighPrecedence = | |
| Translate high { x => EE(x.i) } | |
| } | |
| final case class EE(i: Int) | |
| object EE { | |
| implicit def translate: Translate[EE, FF] with HighPrecedence = | |
| Translate high { x => FF(x.i) } | |
| } | |
| final case class FF(i: Int) | |
| object FF { | |
| implicit def translate: Translate[FF, GG] with HighPrecedence = | |
| Translate high { x => GG(x.i) } | |
| } | |
| final case class GG(i: Int) | |
| object GG { | |
| implicit def translate: Translate[GG, HH] with HighPrecedence = | |
| Translate high { x => HH(x.i) } | |
| } | |
| final case class HH(i: Int) | |
| object HH { | |
| implicit def translate: Translate[HH, II] with HighPrecedence = | |
| Translate high { x => II(x.i) } | |
| } | |
| final case class II(i: Int) | |
| object II { | |
| implicit def translate: Translate[II, JJ] with HighPrecedence = | |
| Translate high { x => JJ(x.i) } | |
| } | |
| final case class JJ(i: Int) | |
| sealed trait HighPrecedence | |
| sealed trait LowPrecedence | |
| object TranslateExample extends App { | |
| import TranslateSyntax._ | |
| println(AA(1).translate[JJ]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment