Skip to content

Instantly share code, notes, and snippets.

@gvolpe
Last active November 3, 2018 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvolpe/b992891490a47dbbd729787c151f229e to your computer and use it in GitHub Desktop.
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
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!
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