Skip to content

Instantly share code, notes, and snippets.

@b-studios
Last active December 19, 2016 10:10
Show Gist options
  • Save b-studios/2db5a3370810e36d8cac973dbdf7d037 to your computer and use it in GitHub Desktop.
Save b-studios/2db5a3370810e36d8cac973dbdf7d037 to your computer and use it in GitHub Desktop.
Autoprojection
// Finds a R in an T
trait Select[T, R] {
def apply(t: T): R
}
trait LowPrio {
implicit def autoLeft[T, R, O]: Select[(R, O), R] = ???
}
object Select extends LowPrio {
implicit def autoRight[T, R, O]: Select[(O, R), R] = ???
}
def select[T, R](t: T)(implicit sel: Select[T, R]): R = sel(t)
val x = (12, "hello")
val y: Int = select(x)
val z: String = select(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment