unexplained "ambiguous implicit arguments" error
See original GitHub issueCompiler version
3.1.1
Minimized code
The setup definitions
object repro:
abstract class Add[VL, VR]:
type VO
def apply(vl: VL, vr: VR): VO
object Add:
// invoking this rule(same-type args) seems to work as expected
transparent inline given [V](using num: scala.math.Numeric[V]): Add[V, V] =
new Add[V, V]:
type VO = V
def apply(vl: V, vr: V): V = num.plus(vl, vr)
// trying to invoke this rule (differing types) causes strange error
transparent inline given [VL, VR](using
nev: scala.util.NotGiven[VL =:= VR],
numL: scala.math.Numeric[VL],
numR: scala.math.Numeric[VR]): Add[VL, VR] =
new Add[VL, VR]:
type VO = Double
def apply(vl: VL, vr: VR): Double = numL.toDouble(vl) + numR.toDouble(vr)
transparent inline def plus[VL, VR](vl: VL, vr: VR)(using add: Add[VL, VR]): add.VO =
add(vl, vr)
Output
scala> import repro.*
scala> plus(1,1) // invoking with identical types works
val res0: Int = 2
scala> plus(1f,1f)
val res1: Float = 2.0
scala> plus(1d,1d)
val res2: Double = 2.0
scala> plus(1,1d) // attempting to trigger the different-types rule errors out unxpectedly
-- Error: ----------------------------------------------------------------------
1 |plus(1,1d)
| ^
|ambiguous implicit arguments of type coulomb.repro.Add[Int, Double] found for parameter add of method plus in object repro.
|I found:
|
| coulomb.repro.Add.given_Add_V_V[V](
| /* ambiguous: both object DoubleIsFractional in object Numeric and object LongIsIntegral in object Numeric match type Numeric[V] */
| summon[Numeric[V]]
| )
|
|But both object DoubleIsFractional in object Numeric and object LongIsIntegral in object Numeric match type Numeric[V].
1 error found
Expectation
I would expect these rules to resolve correctly with no ambiguities
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Unexpected ambiguous implicit argument error - Compiler
I found a strange behavior of compiler. The minimal code snippet that I was able to create is trait Accessor: def get[T](using parser: ......
Read more >Ambiguous implicit values - scala - Stack Overflow
I've been thinking I understand scala implicits until recently faced strange problem. In my application I have several ...
Read more >Implicit arguments — Coq 8.16.1 documentation
An implicit argument of a function is an argument which can be inferred from contextual knowledge. There are different kinds of implicit arguments...
Read more >implicit parameter precedence again - eed3si9n
1, locally declared implicits are preferred over imported ones. The problem is that the spec does not cover such behavior. My original ...
Read more >ambiguous implicit arguments of type sttp.tapir.codec ...
As I've already tried to explain in the comments, the problem is that the method ... lampepfl/dottyunexplained "ambiguous implicit arguments" error#14585.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@erikerlandson here’s a potential workaround.
@armanbilge I sure am grateful we have you in the Scala community. That’s a handy trick to know 🙏
For anyone else following along from home, this is the
apply
that Eric makes reference to above