Ambiguous implicit found: conflicting given instances in Scala 3 trait inheritance
0 reputation · 30 May 2026, 19:11 UTC
0 reputation · 30 May 2026, 19:11 UTC
In Scala 3, the compiler emits an Ambiguous implicit found: … error when more than one given satisfies a required type during implicit search. A common scenario is a class that extends two traits, each providing a given for the same type, while the class also declares its own given. The implicit search considers all three instances, but the compiler cannot decide which one to pick, leading to the ambiguity error.
The current language specification does not specify a clear precedence rule for such cases. It is unclear whether locally defined given instances should trump inherited ones, or if the search should favor the most specific trait in the inheritance hierarchy. This lack of a documented hierarchy creates uncertainty for library authors and users who rely on implicit resolution to work predictably.
Given this, the following questions remain open:
given instances for the same type and also defines its own given?35275 reputation · 31 May 2026, 03:27 UTC
In Scala 3, the Ambiguous implicit found error occurs when the compiler identifies multiple given instances that satisfy a requirement but cannot determine a winner. When a class inherits multiple traits that each provide a given for the same type, and the class itself defines its own given, the search space contains at least three valid candidates.
The Scala 3 implicit search algorithm operates on specificity and scope. While locally defined given instances generally have higher priority than inherited ones, ambiguity arises primarily when two inherited traits provide instances of the same specificity level. Because the compiler does not have a 'tie-breaking' rule for sibling traits, it throws an error to prevent unpredictable behavior.
To resolve this without using compiler hacks, consider the following approaches:
using Clause: The most direct fix is to specify which given instance to use at the call site. This bypasses the search mechanism by providing the instance directly:
def process(using instance: MyType) = { ... }
given definition to a common base trait or a shared object to ensure only one instance enters the scope.given to win, ensure it is the only valid candidate. However, if the inherited instances are still in scope, the compiler may still complain unless the local definition effectively narrows the search path.import statements to bring only specific given instances into the scope where the ambiguity occurs.This explanation assumes you are using standard Scala 3.x implicit resolution. It is important to note that the language specification prioritizes specificity (type depth) but does not guarantee an order of inheritance-based precedence for traits at the same level. If your architecture relies on complex multiple inheritance where given instances are generated dynamically, the behavior may remain fragile across future updates to the compiler's search heuristics.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.