Skip to content

Instantly share code, notes, and snippets.

@Refefer
Last active December 18, 2015 23:29
Show Gist options
  • Save Refefer/5862089 to your computer and use it in GitHub Desktop.
Save Refefer/5862089 to your computer and use it in GitHub Desktop.
Unifying to a type class across view bounds and subtyping
trait GetTC[A,B,T[_]] {
type C
implicit def aToC(a:A):C
implicit def bToC(b:B):C
def get: T[C]
}
trait LowPriorityGetTC {
implicit def getTC1[A,B,T[_]](implicit ac: A => B, tc:T[B]) = new GetTC[A,B,T] {
type C = B
implicit def aToC(a:A):C = ac(a)
implicit def bToC(b:B):C = b
def get = tc
}
}
object GetTC extends LowPriorityGetTC {
implicit def getTC2[A,B,T[_]](implicit ac: B => A, tc:T[A]) = new GetTC[A,B,T] {
type C = A
implicit def aToC(a:A):C = a
implicit def bToC(b:B):C = ac(b)
def get = tc
}
}
// Unifies either across supertypes or view bounds in either direction
// Example Usage:
def lt[A,B](a:A, b:B)(implicit gtc: GetTC[A,B,Ordering]) = {
import gtc._
gtc.get.lt(a,b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment