Last active
December 6, 2016 20:25
-
-
Save stanch/0d6ecc624b924eb629a070f582edd739 to your computer and use it in GitHub Desktop.
When you need to map over an implicit in the current scope
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
case class MapImplicit[A](f: A => A)(implicit current: A) { | |
def in[B](code: A => B) = code(f(current)) | |
} | |
// start with an implicit value | |
implicit val foo: Int = 3 | |
// prints “3” | |
println(implicitly[Int]) | |
// create a new scope with the mapped implicit | |
// note that the name of the new implicit should be exactly the same | |
// (it works by shadowing the old implicit) | |
MapImplicit[Int](_ + 1).in { implicit foo => | |
// prints “4” | |
println(implicitly[Int]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment