Consider a tapir.Codec object (this is how I encountered this problem, but I don't think it has much to do with tapir specifically, it is just about implicit resolution priority in general), it has a bunch of codecs defined for basic types, like Codec.long, Codec.int etc.
I was trying to define a "fallback" codec that could be used when no standard one is found (IRL, the type is more specific than just T, but actual implementation doesn't matter here):
implicit def fallbackCodec[T]: Codec[String, T, PlainText] = ???
But I can't figure out where I can put this definition.
I tried adding it to one of the traits I am extending, or into its own trait, extended by other traits, or into its own object, and just importing Fallback._ at call site ...
Whatever I do, in(path[Foo]("foo")) works beautifully, but in(path[Long]("id")) also ends up using my fallback codec instead of Codec.long
I was thinking that since standard codecs are defined in the companion object, they would trump my custom definition, but looks like that is not the case. Is there some trick I am missing to make it do what I want?
Ok, I think, I found a solution, leaving it here in case someone else finds it helpful. This seems to work:
Another possibility, that also works, but without having to define
defaultCodecand have an extra trait is to explicitlyimport sttp.tapir.Codec._inFooBar.It is bit fragile though, because if author of FooBar forgets to add the explicit import, it will compile, and silently replace all codecs with the fallback. Also, it generates an "unused import" warning.