Implicit Arguments is an actively used design pattern. Arguments that are not passed by the function caller, but are found in the function caller's scope.

?Years Old
  • Have a question about Implicit Arguments not answered here? Email me and let me know how I can help.
  • Read more about Implicit Arguments

Languages with Implicit Arguments include scala

Example from scala:

// https://docs.scala-lang.org/tour/implicit-parameters.html
abstract class Monoid[A] {
  def add(x: A, y: A): A
  def unit: A
}

object ImplicitTest {
  implicit val stringMonoid: Monoid[String] = new Monoid[String] {
    def add(x: String, y: String): String = x concat y
    def unit: String = ""
  }
  
  implicit val intMonoid: Monoid[Int] = new Monoid[Int] {
    def add(x: Int, y: Int): Int = x + y
    def unit: Int = 0
  }
  
  def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
    if (xs.isEmpty) m.unit
    else m.add(xs.head, sum(xs.tail))
    
  def main(args: Array[String]): Unit = {
    println(sum(List(1, 2, 3)))       // uses intMonoid implicitly
    println(sum(List("a", "b", "c"))) // uses stringMonoid implicitly
  }
}

Last updated June 22nd, 2020

Edit Implicit Arguments on GitHub