Scala 3 ambiguous given resolution for using parameters
0 reputation · 04 Apr 2025, 02:31 UTC
0 reputation · 04 Apr 2025, 02:31 UTC
A Scala 3 method declares a context parameter with a using clause, and more than one eligible given instance is visible through local definitions, imports, or the implicit scope of the expected type. The compiler reports ambiguous implicit values instead of selecting one instance.
The goal is to diagnose which candidate givens the compiler is considering and why no single instance wins. Resolution behavior can differ by Scala 3 minor version, so the applicable implicit search rules depend on the project's scalaVersion. The compiler does not defer this choice to runtime, and subtype specificity alone does not always disambiguate without explicit priority annotations.
Specific questions:
34775 reputation · 04 Apr 2025, 07:41 UTC
The compilation unit is governed by the scalaVersion declared for the project. Implicit / given search rules are version-specific in Scala 3, so the first diagnostic step is to confirm the exact minor version used to compile the file.
Imported givens and companion-object givens both enter the candidate set for a using parameter. There is no built-in language priority that automatically prefers an import over a companion object or vice versa. When two candidates are equally specific, the compiler reports ambiguity and does not defer the choice to runtime.
Scala 3 does not provide explicit given priority annotations. Disambiguation is achieved by narrowing the search scope, e.g., importing only the desired given, removing a conflicting given from scope, or making one candidate strictly more specific in type.
The ambiguity you see is most likely a scope collision: at least two givens of the same type are visible simultaneously through a local definition, an import, and/or a companion object. Without a specificity difference, the compiler cannot choose.
scalaVersion in build.sbt / build.sc. The search order and diagnostics differ between 3.1, 3.2, 3.3+.scalaVersion and the same imports.-Xprint:typer to see which given instances the typer considers.One missing diagnostic detail that changes the recommendation: the exact compiler error text listing the candidate given names and their defining symbols, together with the confirmed scalaVersion. With those, the specific search phase that is colliding can be identified.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 04 Apr 2025, 10:19 UTC
To build on the previous answer, it is important to note that while Scala 3 doesn't have a priority annotation, the lexical scope acts as a primary tie-breaker. A given defined in the immediate local scope (e.g., inside a method) will be preferred over one imported from another package or defined in a companion object.
If you are facing ambiguity between two imported givens or a companion given and an import, consider these practical verification steps:
import com.example.givens.* with import com.example.givens.specificGiven to prune the candidate set.using clause explicitly: myMethod(using preferredInstance).given is defined as a more specific subtype of the expected type, as the most specific type typically wins.