Scala 3 Givens and Using Clauses: Explicit Context Without Implicit Noise
Scala 3 separates the implicit keyword into given and using, making context dependencies explicit, type-safe, and easy to thread through application layers.
05 Sept 2026, 22:24 UTC

The Problem: Implicit Noise
In Scala 2, the implicit keyword handled default arguments, type class instances, and dependency injection alike, creating an implicit soup where a parameter could be any of those without clear indication, forcing developers to hunt through imports and companion objects.
Scala 3 splits this into two keywords: given and using. This separation makes context explicit at the definition site while keeping the call site concise, and the compiler enforces resolution at build time.
Defining a Context with given
A given is a value the compiler can automatically provide to any method requesting that specific type. Best practice is to place given instances in the companion object of the type they support, so the compiler finds them without explicit imports.
Requesting Context with using
To consume a given, add a using clause to the method signature. The compiler requires a matching given in scope; if missing, it errors at compile time, preventing runtime missing-configuration bugs.
Worked Example: Threading a Request Context
case class RequestContext(traceId: String, userId: String)
def logAction(msg: String)(using ctx: RequestContext): Unit = println("[Trace: " + ctx.traceId + "] " + msg)
given defaultRequestCtx: RequestContext = RequestContext("system-trace", "system-user")
@main def run(): Unit = logAction("Application started")If the given is omitted, the compiler stops with a No given instance error, proving the dependency is strictly enforced.
Trade-offs and Checklist
- Use
givenfor cross-cutting concerns like logging, auth, or config, not core domain logic. - Place instances in companion objects to minimize import noise.
- If a method needs more than three context parameters, group them into a single
case class. - Compile with
-Xlintto catch unused givens or potential ambiguities early.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.