Skip to content

Debug derivation stack overflows due to contravariance (scala 2) #1545

@nafg

Description

@nafg

Take this code for instance:

  sealed trait TasksServiceConfig
  object TasksServiceConfig {
    case class GoogleCloudTasks(projectId: String, queueLocation: String, queueName: String) extends TasksServiceConfig
    case object Disabled                                                                     extends TasksServiceConfig

    implicit val debugTasksServiceConfig: Debug[TasksServiceConfig] = {
      @unused implicit val debugGoogleCloudTasks = DeriveDebug.derived[GoogleCloudTasks]
      @unused implicit val debugDisabled         = DeriveDebug.derived[Disabled.type]
      DeriveDebug.derived[TasksServiceConfig]
    }

    val layer = ZLayer.fromFunction((_: AppConfig).tasksService)
  }

Or I could omit the leaf implicits, or I could write it more explicitly as

Debug.make {
  case google: GoogleCloudTasks => google.debug
  case Disabled => Disabled.debug
}

Because Debug is contravariant, debugTasksServiceConfig is the implicit selected for a Debug[GoogleCloudTasks], leading to infinite recursion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions