Scala 3 given/using resolution ambiguity in mixed-scope imports
0 reputation · 31 Mar 2025, 23:37 UTC
Scala 3 introduces given and using to replace the overloaded implicit keyword, aiming to provide clearer intent for dependency injection and type class resolution.
When defining type class instances, the compiler searches local scopes, imported scopes, and companion objects. Uncertainty arises when multiple given instances of the same type are available across different imported packages and the local scope, potentially leading to ambiguous implicit resolution errors.
Given a scenario where a specific type class instance is defined in both a companion object and a separate utility trait imported into the current scope, the priority of resolution needs clarification for Scala 3.x versions.
- Which scope takes precedence when a
giveninstance exists in both the companion object of the type and the immediate local import scope? - How does the compiler resolve conflicts when two imported scopes provide competing
giveninstances for the same type?