Forked from missingfaktor/rich-mans-type-classes.scala
Last active
August 29, 2015 14:08
-
-
Save nlinker/6bb169fa0459486dcbf3 to your computer and use it in GitHub Desktop.
This file contains 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 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