Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support heterogenous typeclass derivation #503

Open
gpoulin opened this issue Dec 19, 2023 · 0 comments
Open

Support heterogenous typeclass derivation #503

gpoulin opened this issue Dec 19, 2023 · 0 comments

Comments

@gpoulin
Copy link

gpoulin commented Dec 19, 2023

There's cases where the typeclass we want to derive from a case class is a super-type of the typeclass. One such example is the JSON encoding typeclass in play-json which look likes the following:

trait Writes[A] {
  def writes(o: A): JsValue
}

trait OWrites[A] extends Writes[A] {
  def writes(o: A): JsObject
}

In this example, OWrites is a Writes that guarantees that the JsValue is actually a JsObject. I would like to be able to derive the OWrites typeclass on case class using Magnolia. AFAIK, this is currently not possible since the parameters of the case class don't always have an implementation for OWrites, but sometime just an implementation for Writes.

Apriori (I'm likely missing something), making a small change to the interface would make that possible for scala 3

trait CommonHeteroDerivation[TypeClass[_], ParamTypeClass[_]]:
  def join[T](caseClass: CaseClass[ParamTypeclass, T]): Typeclass[T]
end CommonHeteroDerivation

trait CommonDerivation[TypeClass[_]] extends CommonHeteroDerivation[TypeClass, TypeClass]

The change for scala 2 would be more involved.

The same concept could be applied to sealed trait derivation, but the use cases are less obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant