Skip to content

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

@neko-kai

Description

@neko-kai

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions