Open nevillelyh opened 4 years ago
@nevillelyh You can stick a shapeless.LowPriority
guard in front of derivation to force it to happen only once every other implicit is tried https://github.com/propensive/magnolia/issues/107#issuecomment-589605500
^ LowPriority
macro is heavily used in scalacheck-shapeless for the same purpose, i suggest studying it's source code if you have more questions
If you don't want to pull shapeless just for that you can use a simple trick (Refute
).
See for example in kittens
: https://github.com/typelevel/kittens/blob/master/core/src/main/scala/cats/derived/package.scala#L34-L36
Refute
is also available in shapeless, but it's really small: https://github.com/milessabin/shapeless/blob/master/core/src/main/scala/shapeless/refute.scala
Thanks both I'll take a look!
This is more of a question for the group.
I made a library that derives common type classes that we use, one of the is cats. https://github.com/spotify/magnolify/tree/master/cats/src/main/scala/magnolify/cats
I want it to be usable out of the box by those not familiar with magnolia, implicit resolution rules, etc. The idea is that users can simply add the following 2 imports, and have type classes instances for everything:
The 2nd import goes through some macros that redirect to
macro Magnolia.gen[T]
like this one https://github.com/spotify/magnolify/blob/master/cats/src/main/scala/magnolify/cats/auto/CatsMacros.scalaHowever I get error messages like this:
Basically Cats already have implicit instances for
List[T]
,Option[T]
, etc. which are also sealed traits and can be derived with Magnolia. I worked around this by messing with implicit priorities like this, but am wondering if there a better way of handling this?