This document discusses Scala implicits, which allow for implicit conversions and implicit parameters. It covers two main features:
1. Implicit conversions, which allow automatic conversions between types when needed. Implicits are searched for in the implicit scope, including imported values and companion objects.
2. Implicit parameters, where the last parameter group of a function can be marked implicit. The compiler will search for an implicit value to pass to the function. This allows for dependency injection without explicit arguments.
Higher-kinded types can also be used with implicit parameters. The implicit search scope determines where implicits may be found and includes local variables, imported values, and companion objects.
22. Implicit Search Scope
• Similar to value resolution scope
• local variables, values in current object,
super classes.
• Imported object members
• Function parameters
23. Implicit Search Scope
// COMPANION OBJECTS
trait Monoid[T]
object Monoid { // SAME NAME
implicit val s = new Monoid[String]
}
// `s` will be available anywhere a
Monoid[String] is needed. Without imports.
24. Implicit Search Scope
def foo[T](t: T)(implicit m: Monoid[T]) = {
// m is available for any implicit
parameters needed within this method
body
}
25. More!
• Anonymous functions
• implicit r => { ... }
• Import instances
• case class Z(a: A) { implicit ia = a }
• val z = new Z(someA)
• import z._