Last active
November 3, 2018 06:37
-
-
Save gvolpe/b992891490a47dbbd729787c151f229e to your computer and use it in GitHub Desktop.
Alternative encoding of https://gist.github.com/gvolpe/fba7efc41e924bb194e6eaabac309a09 using the Polymorphic library
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
import polymorphic._ | |
import polymorphic.syntax.all._ | |
class Data[A](val x: A, val f: A => String) | |
trait E { | |
def f(ex: ∃[Data]): String = ex match { case Exists(d) => d.f(d.x) } | |
} | |
val e = new E {} | |
e.f(∃(new Data[Int](123, _.toString))) // res3: String = 123 | |
e.f(∃(new Data[String]("Hello!", _.toUpperCase))) // res4: String = HELLO! |
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
import polymorphic._ | |
import polymorphic.syntax.all._ | |
class Data[A](val x: A, val f: A => String) | |
trait E { | |
def f: String | |
} | |
def ex(implicit ev: ∃[Data]): String = ev match { case Exists(x) => x.f } | |
val e1 = new E { | |
override def f = ex(∃(new Data[Int](123, _.toString))) | |
} | |
val e2 = new E { | |
override def f = ex(∃(new Data[String]("Hello!", _.toUpperCase))) | |
} | |
e1.f // res3: String = 123 | |
e2.f // res4: String = HELLO! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment