Unexpected ambiguous implicit argument error

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: Parser[T]): T = parser(this)

type Parser[T] = Accessor => T

trait Configurable

given ParserString: Parser[String] = ???

given ParserConfigurable[T <: Configurable]: Parser[T] = ???

given ParserInt: Parser[Int] = accessor => Integer.parseInt(accessor.get[String])

The error is

[error] 12 |given ParserInt: Parser[Int] = accessor => Integer.parseInt(accessor.get[String])
[error]    |                                                                                ^
[error]    |ambiguous implicit arguments: both given instance ParserString and given instance ParserConfigurable match type Parser[String] of parameter parser of method get in trait Accessor
[error] one error found
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

Probably it is my misunderstanding how generic generic given instances should work, but for me this piece of code should be compiled because string does not implement Configurable, and therefore ParserConfigurable should not match type Parser[String].

Could you please kindly tell me if I met a compiler bug or it is an expected behavior and I need to change my code?

That’s because underlying function in type Parser[T] = Accessor => T has covariant result, so both of given gets same inference as AnyRef, if I’m right. In order to fix that you need to define Parser as invariant:

trait Parser[T] {
  def apply(accessor: Accessor): T

Thank you very much! In the real code Parser is a partial function. Probably inheritance like

class Parser[T] extends PartialFunction[Accessor, T]

could help.