How can I hide my implicit method or disable "LabelledGeneric" for a specific sealed family?
I need to use third party traits that are designed as:
trait Super[T] trait Sub[T] extends Super[T]
and I have defined the following method for providing instances for arbitrary sealed families:
implicit def subFor[T, Repr]( implicit gen: LabelledGeneric.Aux[T, Repr], sg: Lazy[JsonFormat[Repr]] : Sub[T] = ???
but somewhere else in the code, someone has already defined
implicit def superFor[T]: Super[T] = ???
for some specific types, eg.
and several others.
In fact, I would like to use existing implementations instead of using my standard implementation
. However, since my method returns
and returns an existing implementation,
I see no way to define my implementation in such a way that it is considered a lower priority.
How can I block mine
from being called for certain types? Is there a way to disable
for specific types, or some kind of priority reduction mechanism that the compiler from looking for mine
if it already sees
source to share
If implicit defs are defined in companion objects, you must resort to collision-based type computation. But for "some concrete types" defs are usually not found in companion class objects (eg, not on
). In this case, you can take advantage of the fact that the implications hide each other within a specific namespace. So you can simply
implicit def superFor[T]: SomeThrowawayType = ???
will no longer work. Since you are changing the return type, it won't collide or anything else; he actually just disappeared. Then you can create your own forwarders at whatever priority level you want to explicitly call their defs.
This requires, however, that you import your things after them (so they shadow them). If this seems too difficult to rely on, you have to look for type computations.
source to share