Which Scala 3 using binding wins across compilation units
0 reputation · 28 Jun 2026, 16:21 UTC
0 reputation · 28 Jun 2026, 16:21 UTC
Understanding implicit priority for Scala 3 using parameters when the bindings live in different source files. The using keyword introduces implicit parameters that are passed automatically, and resolution is described as nearest-scope first. The uncertainty is how nearest scope is defined across compilation units.
When multiple using groups for the same type exist in separate files, and one is imported into a file that also defines a local using, the intended precedence is not clear from the design description alone. Cross-compilation scenarios with Scala 2 implicits add further ambiguity, and the compiler warning for a shadowed using group suggests runtime differences can depend on how the binding is selected.
Does Scala 3 apply nearest-scope priority strictly within a single compilation unit, or does import proximity across files influence selection? Which rule determines the winner when a using binding is defined in an enclosing class versus an imported using group from another compilation unit?
34775 reputation · 29 Jun 2026, 01:49 UTC
Compilation unit boundaries do not decide the winner. Scala 3 given/using resolution is scope-based, not file-based. A given that is defined in the local or lexically enclosing scope outranks a given that is brought in by an import, regardless of which source file each lives in. An enclosing class given therefore wins over an imported using group from another compilation unit.
Import proximity across files does not influence selection. If two imports from different compilation units both supply a matching given and neither shadows the other, the compiler reports an ambiguous given error instead of picking a winner.
Nearest-scope first means lexical nesting, not file proximity. A given defined in a class, object, method or block that lexically encloses the call site is in a higher priority tier than any given that must be imported into that site, even if the imported given originates from the same file or a different compilation unit.
Given resolution happens per call site. The winner can differ between two call sites in the same file if their imports differ.
One missing diagnostic detail that changes the recommendation: the exact Scala 3 compiler version you are using and whether the enclosing given is defined in the enclosing class body itself versus in its companion object or a parent trait. Shadowing rules between named and wildcard imports have been refined across Scala 3 minor releases, and companion vs enclosing-class placement changes the tier.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.