Last active
August 29, 2015 14:05
-
-
Save mrvisser/a7f0eb4201bbbc009135 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
object Main { | |
trait ~>[F[_], G[_]] { | |
def apply[T](t: F[T]): G[T] | |
} | |
// ERROR: Compiler doesn't think this implements `def apply[T](t: F[T]): G[T]` :( | |
class Poly1[F[_], G[_]] extends ~> { | |
private def implicitApply[T](t: F[T])(implicit f: F[T] ~> G[T]): G[T] = f(t) | |
def apply[T](t: F[T]): G[T] = implicitApply(t) | |
} | |
def main(args: Array[String]): Unit = { | |
type Id[T] = T | |
type Const[C] = { | |
type I[T] = C | |
} | |
def singleton = new (Id ~> Set) { | |
def apply[T](t: T): Set[T] = Set(t) | |
} | |
def identity = new (Id ~> Id) { | |
def apply[T](t: T): T = t | |
} | |
def headOption = new (List ~> Option) { | |
def apply[T](t: List[T]): Option[T] = t.headOption | |
} | |
def size = new (Id ~> Const[Int]#I) { | |
def apply[T](t: T): Int = 0 | |
} | |
println(singleton(List(1, 2, 3))) | |
println(singleton("7")) | |
println(identity(List(1, 2, 3))) | |
println(identity("7")) | |
println(headOption(List(1, 2, 3))) | |
println(size(List(1, 2, 3))) | |
println(size("7")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment