Languages Features Calendar About Lists Add Language
GitHub icon

Implicit Arguments

Implicit Arguments

Implicit Arguments are a language feature.

Arguments that are not passed by the function caller, but are found in the function caller's scope.

Languages with Implicit Arguments include Scala

Example from Scala:

// 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 } }

Article source

PLDB - Build the next great programming language. v5.0.0 - Acknowledgements · Email · GitHub