Consider the default codec as offered in the io
package.
implicitly[io.Codec].name //res0: String = UTF-8
It's a "low priority" implicit so it's easy to override without ambiguity.
implicit val betterCodec: io.Codec = io.Codec("US-ASCII")
implicitly[io.Codec].name //res1: String = US-ASCII
It's also easy to raise its priority level.
import io.Codec.fallbackSystemCodec
implicit val betterCodec: io.Codec = io.Codec("US-ASCII")
implicitly[io.Codec].name //won't compile: ambiguous implicit values
But can we go in the opposite direction? Can we create a low level implicit that disables ("ambiguates"?) the default? I've been looking at the priority equation and playing around with low priority implicits but I've yet to create something ambiguous to the default.
Sort of, yes.
You can do this by creating a 'newtype'. I.e. a type that is simply a proxy to
io.Codec
, and wraps the instance. This means that you also need to change all your implicit arguments fromio.Codec
toCodecWrapper
, which may not be possible.If I understand correctly you want to check at compile time that there is local implicit
io.Codec
("higher-priority") or produce compile error otherwise. This can be done with macros (using compiler internals).Tested in 2.13.0.