Last active
July 11, 2018 06:53
-
-
Save xuwei-k/118230f49c6522bddfcf1ef137c11945 to your computer and use it in GitHub Desktop.
byname implicit parameter🎉 https://github.com/scala/scala/pull/6050
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
resolvers += "scala-pr" at "https://scala-ci.typesafe.com/artifactory/scala-integration/" | |
scalaVersion := s"2.13.0-pre-874bf03" | |
scalacOptions += "-language:_" |
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
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