Scala: Implicit parameters when to use them?

Click for: original source

Implicits are one of the most feared features of the Scala programming language and for good reasons! By Julien Truffaut.

First, the concept of implicits is fairly specific to Scala. No other mainstream programming language has a similar concept. This means that new Scala developers have no patterns to rely on to use implicits correctly.

Second, the keyword implicit is overused in Scala 2 (similar to _). Therefore, it requires a lots of time and practice to distinguish between the various usages of implicits. On that point, Scala 3 has made great improvements by introducing dedicated syntax for each implicit’s use case.

The article then goes over:

  • Usages
  • The environment pattern
  • Bonus: Alternative implementations of the environment pattern
    • Class parameters
    • ThreadLocal
    • Reader
    • ZIO

The most important take away about implicit parameters is that values injected by the compiler should be obvious! If you need to check your imports or run a debugger to figure out which value was injected, then it is not obvious and you would be better off passing the values explicitly. Nice one!

[Read More]

Tags scala programming akka serverless