Created
May 2, 2012 09:07
-
-
Save missingfaktor/2575397 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 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