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

Can't summon Generic instance for T within T's companion object: error super constructor cannot be passed a self reference #837

Open
neko-kai opened this issue Jun 14, 2018 · 3 comments
Labels

Comments

@neko-kai
Copy link

neko-kai commented Jun 14, 2018

As an example, the following code breaks:

import shapeless._

case class Abc(x: Int)

object Abc extends WithGeneric[Abc]

abstract class WithGeneric[T: Generic] {
  val generic: Generic[T] = implicitly[Generic[T]]
}

with error:

Error:(9, 21) super constructor cannot be passed a self reference unless parameter is declared by-name
object Abc extends WithGeneric[Abc]
                   ^

Note that changing the name of the object makes it work:

object NotAbc extends WithGeneric[Abc]

Our use case for this is that we want to be able to define helper traits that provide circe & pureconfig codec instances for case classes:

abstract class WithCirce[A: DerivedEncoder: DerivedDecoder] {
  implicit val enc: Encoder[A] = implicitly[DerivedEncoder[A]]
  implicit val dec: Decoder[A] = implicitly[DerivedDecoder[A]]
}

Both circe and pureconfig are based on shapeless, so right now we can't write this and have to workaround by using Cached[Lazy[_]]. Non-shapeless based derivations work as expected however.

@milessabin
Copy link
Owner

milessabin commented Jun 14, 2018

The trouble is the materialized Generic[T], where T is instantiated at a case class, will embed a reference to the case class companion. Hence your construct object Abc extends WithGeneric[Abc] is self-referential in a way that's not easily fixed.

It's just conceivable that byname implicits in 2.13.x will make it possible to make some progress, but for now I don't think there's a lot that can be done.

@joroKr21
Copy link
Collaborator

It's just conceivable that byname implicits in 2.13.x will make it possible to make some progress, but for now I don't think there's a lot that can be done.

No progress unfortunately

@joroKr21
Copy link
Collaborator

This could be mitigated if case classes use constructors and field access instead of the companion's apply / unapply but it's tricky to do. I remember attempting that at some point and it didn't work.

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

No branches or pull requests

3 participants