Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active July 11, 2018 06:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/118230f49c6522bddfcf1ef137c11945 to your computer and use it in GitHub Desktop.
Save xuwei-k/118230f49c6522bddfcf1ef137c11945 to your computer and use it in GitHub Desktop.
byname implicit parameter🎉 https://github.com/scala/scala/pull/6050
resolvers += "scala-pr" at "https://scala-ci.typesafe.com/artifactory/scala-integration/"
scalaVersion := s"2.13.0-pre-874bf03"
scalacOptions += "-language:_"
package example
trait Eq[A] {
def equal(x: A, y: A): Boolean
}
final case class Cofree[F[_], A](head: A, tail: F[Cofree[F, A]])
object Cofree {
implicit def cofreeEq[F[_], A](implicit a: Eq[A], f: => Eq[F[Cofree[F, A]]]): Eq[Cofree[F, A]] =
(x, y) => {
a.equal(x.head, y.head) && f.equal(x.tail, y.tail)
}
}
object Main {
implicit def intEq: Eq[Int] = _ == _
implicit def optionEq[A](implicit a: Eq[A]): Eq[Option[A]] = {
case (Some(x), Some(y)) =>
a.equal(x, y)
case (None, None) =>
true
case (_, _) =>
false
}
def main(args: Array[String]): Unit = {
val c: Cofree[Option, Int] = Cofree(1, Option(Cofree(2, Option(Cofree[Option, Int](3, None)))))
println(implicitly[Eq[Cofree[Option, Int]]].equal(c, c))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment