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

genericObjectEncoder does not work #46

Open
khanetor opened this issue May 29, 2017 · 2 comments
Open

genericObjectEncoder does not work #46

khanetor opened this issue May 29, 2017 · 2 comments

Comments

@khanetor
Copy link

The implicit function genericObjectEncoder does not work with Coproduct as descibed in the book.

The current definition is as followed:

implicit def genericObjectEncoder[A, H <: HList](
                                                    implicit
                                                    gen: LabelledGeneric.Aux[A, H],
                                                    hEncoder: Lazy[JsonObjectEncoder[H]]
                                                  ): JsonEncoder[A] =
    createObjectEncoder(value => hEncoder.value.encode(gen.to(value)))

To make this work, I have to remove <: HList.

I do not know why this is the case, but I would love to find out more about it.

Other users reported knownDirectSubclasses bug, but I do not see this error message.

@SystemFw
Copy link

SystemFw commented Jun 2, 2017

As I mentioned on gitter, the reason for this is that the LabelledGeneric for A could either be an HList (for case class), or a Coproduct of HLlists if A is a sealed trait with subclasses. We want genericObjectEncoder to work for both, but implicit resolution will neglect to consider it for a Coproduct, because of the H <: HList constraint. Removing it does the trick.

I agree that the book should be amended, I'm happy to make a PR with a small paragraph, but I'm not too sure who the maintainers for this project are, and if they're accepting pull requests.

The knownDirectSubclasses is unrelated to the code in the example, it appears in random cases whenever you try to derive a LabelledGeneric. I think the problem is only definitely solved by enum in Dotty.

@davegurnell
Copy link
Collaborator

davegurnell commented Jun 5, 2017

@SystemFw hit the nail on the head -- the <: HList annotation shouldn't be there. I'll incorporate this correction in next time I fold errata into the book.

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

3 participants