In a project I’m working on, I have a project with a given instance looking like this (simplified):
class Test[A, B]
inline given [A, B](using A): Test[A, Test[A, B]] = new Test
This works as intended when summoning something like
given Int = ???
given String = ???
summon[Test[Int, Test[Int, String]]] //OK
But if I change generics:
given Int = ???
given String = ???
summon[Test[Int, Test[String, Int]]] //Ambiguous implicit argument between given_Int and given_String
Since Test[Int, Test[String, Int]]
does not match Test[A, Test[A, B]]
, I would expect the compiler to just ignore the above given and use another/give a “no suitable given found” error.
After doing some tests, it works if I remove the using A
:
inline given [A, B]: Test[A, Test[A, B]] = new Test
summon[Test[Int, Test[Int, String]]] //OK
summon[Test[Int, Test[String, Int]]] //Search for another given instance
It seems that the compiler tries to evaluate the using A
parameter first using ?
since Test[?, Test[?, ?]]
matches the type Test[A, Test[A, B]]
and ?
is a (as far as I know) supertype of Int
and String
.
I prefer posting here first before opening an issue on Github.
Is this the correct behaviour? If yes, how can I make the compiler just “skip” this given in the second case instead of checking with A = ?
and B = ?
?