Implicits in Scala

If there is one feature that makes Scala “Scala”, I would pick implicits. – Martin Odersky

Implicits in Scala

  • Language feature
  • Allow omitting method calls or variable references
  • Compilation safety

Implicits in Scala

  • Values labeled with an implicit modifier can be passed to implicit parameters and used as implicit conversions
  • implicit is an illegal modifier for top-level objects

The compiler does not try to apply implicits if the code typechecks!

Implicit conversions

  • Historically came first in the language
  • Allow arbitrary classes to implement new interfaces
  • Prefer avoiding implicit conversions unless you have a very good reason to use them
  • Scala 3 will restrict usage of implicit conversions

Implicit conversions

An implicit conversion from type A to type B is defined by an implicit value that has a type signature S => T or (=> S) => T

Implicit conversions

  • scala.Predef

Implicit conversions

Implicit conversions

Implicit conversions

Implicit conversions

Rules

Marking

  • Definitions must be explicitly marked implicit to become available
  • Variables, functions and object definitions can be marked implicit

Scope

  • An implicit conversion must be in scope as a single identifier
  • Otherwise the compiler will not consider it
  • Companion objects bring member implicits in scope

Resolution order

  • Current scope
  • Imports
  • Companion objects
  • Parameter arguments
  • Type parameters
  • Outer objects of nested types
  • Parent objects

One at a time

  • The compiler only considers a single implicit insertion at a time
  • This is done for compilation performance considerations
  • It is possible to circumvent this

Resolving ambiguity

  • If there are more than one matching implicits, the most specific one is chosen
  • If there is no unique most specific candidate, a compile error is reported

When does the compiler try to apply implicits?

  • Implicit conversion to an expected type
  • Implicit conversion of a method call receiver
  • Implicit parameters

Implicit conversion to an expected type

Implicit conversion of a method call receiver

Type interop

Type interop

Simulate syntax

Implicit classes

  • Syntactic sugar for defining a class together with an implicit conversion

becomes

Implicit classes

Implicit classes - Rich wrapper pattern

Implicit value classes

Implicit parameters

Applicability: - Type classes - Context and config - Dependency injection - Proving theorems - …

Implicit parameters

Proving theorems

Providing context using implicits

Don’t use implicits for conveniently passing ordinary parameters around!

Traditional ways to express context

Imperative way

  • Global/shared mutable variables
  • Global/shared constants
  • Mutability is dangerous, constants are rigid

Functional way

  • Just pass anything you need as a parameter
  • Type safe
  • Sometimes gets tedious
  • Error prone

Scala way

  • Leave some parameters implicit
  • We provide just a type
  • Compiler provides the rest

Scala way

Cool stuff

  • :implicits
  • -Xprint:typer
  • @implicitNotFound

Ordering example

Supplying implicit values

Supplying implicit values