Skip to content

Instantly share code, notes, and snippets.

@stanch
Last active December 6, 2016 20:25
Show Gist options
  • Save stanch/0d6ecc624b924eb629a070f582edd739 to your computer and use it in GitHub Desktop.
Save stanch/0d6ecc624b924eb629a070f582edd739 to your computer and use it in GitHub Desktop.
When you need to map over an implicit in the current scope
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