Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nlinker/6bb169fa0459486dcbf3 to your computer and use it in GitHub Desktop.
Save nlinker/6bb169fa0459486dcbf3 to your computer and use it in GitHub Desktop.
trait Isomorphism[A, B] {
def fw: A => B
def bw: B => A
}
object Isomorphism {
implicit def anyIso[A](a: A) = new {
def as[B](implicit ev: Isomorphism[A, B]) = ev fw a
}
implicit def isoX[A] = new Isomorphism[(Unit, A), A] {
def fw = _._2
def bw = ((), _)
}
}
trait Storable[A] {
def size: Int
}
object Storable {
implicit object IntIsStorable extends Storable[Int] {
def size = 4
}
def size[A](implicit s: Storable[A]) = s.size
}
object Main {
import Storable._
import Isomorphism._
def main(args: Array[String]): Unit = {
println(size[Int])
println(((), 'hello).as[Symbol])
}
}
/* Output:
4
'hello
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment