Implicit Parameters
Context passing.
Implicit Parameters is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Passing context automatically
Implicit parameters let the compiler supply an argument for you, drawn from values marked implicit in scope. They are great for passing 'ambient' context like configuration or formatting rules.
object Main {
def greet(name: String)(implicit greeting: String): String =
s"$greeting, $name!"
implicit val defaultGreeting: String = "Hello"
def main(args: Array[String]): Unit = {
println(greet("Ann"))
}
}The implicit parameter list
Mark the last parameter list with the implicit keyword. When you omit it at the call site, the compiler searches for a matching implicit value.
object Main {
def multiply(x: Int)(implicit factor: Int): Int = x * factor
implicit val f: Int = 10
def main(args: Array[String]): Unit = {
println(multiply(5))
}
}Providing implicits explicitly
You can always pass an implicit parameter explicitly, overriding the one in scope. This is handy for one-off cases.
object Main {
def multiply(x: Int)(implicit factor: Int): Int = x * factor
implicit val f: Int = 10
def main(args: Array[String]): Unit = {
println(multiply(5))
println(multiply(5)(2))
}
}Implicit resolution by type
The compiler matches implicits by type, not by name. There must be exactly one matching implicit value in scope, or compilation fails as ambiguous or missing.
object Main {
case class Config(env: String)
def run()(implicit cfg: Config): String = s"running in ${cfg.env}"
implicit val cfg: Config = Config("prod")
def main(args: Array[String]): Unit = {
println(run())
}
}implicitly to summon a value
The implicitly[T] helper fetches the implicit value of type T from scope directly, without naming a parameter.
object Main {
implicit val name: String = "Scala"
def main(args: Array[String]): Unit = {
val n = implicitly[String]
println(n)
}
}Context bound shorthand
A context bound [T: Ordering] is sugar for an implicit parameter of type Ordering[T]. It is common with type classes.
object Main {
def max[T: Ordering](a: T, b: T): T = {
val ord = implicitly[Ordering[T]]
if (ord.gt(a, b)) a else b
}
def main(args: Array[String]): Unit = {
println(max(3, 7))
println(max("apple", "pear"))
}
}Threading context through calls
Implicit parameters shine when many functions need the same context. Mark it implicit once and it flows through nested calls automatically.
object Main {
case class User(name: String)
def log(msg: String)(implicit u: User): Unit =
println(s"[${u.name}] $msg")
def doWork()(implicit u: User): Unit = {
log("started")
log("finished")
}
implicit val current: User = User("admin")
def main(args: Array[String]): Unit = {
doWork()
}
}Where implicits are searched
The compiler looks for implicits in the local scope, in imports, and in the companion objects of the involved types. Companion objects are a tidy place to provide defaults.
object Main {
case class Currency(symbol: String)
object Currency {
implicit val default: Currency = Currency("$")
}
def price(amount: Int)(implicit c: Currency): String = s"${c.symbol}$amount"
def main(args: Array[String]): Unit = {
println(price(50))
}
}Ambiguity errors
If two implicits of the same type are in scope, the compiler reports an ambiguous implicit error. Keep at most one per type, or pass explicitly.
object Main {
def label(x: Int)(implicit unit: String): String = s"$x$unit"
implicit val unit: String = "px"
def main(args: Array[String]): Unit = {
// Only one implicit String in scope -> no ambiguity
println(label(12))
}
}Default-like behavior
Implicit parameters give a form of context-aware defaults: the same function call yields different results depending on which implicit is in scope.
object Main {
def format(n: Double)(implicit decimals: Int): String =
f"$n%.${decimals}f"
implicit val d: Int = 2
def main(args: Array[String]): Unit = {
println(format(3.14159))
println(format(3.14159)(4))
}
}Use implicits sparingly
Implicit parameters are powerful but can make code harder to follow if overused. Reserve them for genuine cross-cutting context like execution contexts, configs, and type-class instances.
object Main {
case class Locale(code: String)
def hello()(implicit loc: Locale): String =
if (loc.code == "tr") "Merhaba" else "Hello"
implicit val loc: Locale = Locale("tr")
def main(args: Array[String]): Unit = {
println(hello())
}
}Quick Check
How does the compiler decide which value to supply for an implicit parameter?
Recap
You learned implicit parameters (Scala 2 style):
- Mark a parameter list
implicitto have it filled automatically - Resolution is by type, must be unique in scope
implicitly[T]summons a value; context bounds[T: TC]are sugar- Search covers local scope, imports, and companion objects
- Use them for cross-cutting context, not everything
Frequently asked questions
Is the “Implicit Parameters” lesson free?
Yes — the full text of “Implicit Parameters” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Implicit Parameters”?
Context passing. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Implicit Parameters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Implicit Parameters
- Scala 3 given/using
- Implicit Conversions
- Extension Methods