This project contains a Scala compiler plugin which adds support for locally scoped implicit values.
For example:
imply("Hello, world!") { implicitly[String] }
expands to:
{
implicit val implied$0 = "Hello, world!"
implicitly[String]
}
which at runtime, evaluates to "Hello, world!"
.
The imply
method takes two parameter lists -- the first parameter list contains 1 or more values to declare in a local implicit scope, and the second parameter list contains a block to evaluate in context of the local implicit scope.
Defining implicits in local scope with vanilla Scala is problematic for a number of reasons.
The imply
method is particularly useful when working with type class instances that have multiple lawful implementations. For example, consider some monoid instances for Int
:
2 |+| 3 // 5
imply(intMultiplication) { 2 |+| 3 } // 6
imply(minMonoid) { 2 |+| 3 } // 2
imply(maxMonoid) { 2 |+| 3 } // 3
This plugin currently supports Scala 2.10 and 2.11.
To use this plugin with SBT, add the following to build.sbt:
addCompilerPlugin("com.github.mpilquist" %% "local-implicits" % "0.3.0")