类型不匹配;发现:cats.Show[shapeless.CNil] 要求:cats.Show[A] 在 sbt 中添加 scalac 选项 -Ypartial-unification 之后

type mismatch; found : cats.Show[shapeless.CNil] required: cats.Show[A] after adding scalac option -Ypartial-unification in sbt

我有以下代码。

import enumeratum.{Enum, EnumEntry}

sealed abstract class AppEnvironment extends EnumEntry

object AppEnvironment extends Enum[AppEnvironment] {
  case object Local extends AppEnvironment
  case object Testing extends AppEnvironment
  case object Production extends AppEnvironment

  override val values: Vector[AppEnvironment] =
    findValues.toVector
}

import java.net.InetAddress
import ciris.Secret
import eu.timepit.refined.types.net.UserPortNumber
import scala.concurrent.duration.Duration

final case class ApiConfig(
  host: InetAddress,
  port: UserPortNumber,
  apiKey: Secret[ApiKey],
  timeout: Duration
)


import java.net.InetAddress
import cats.Show
import cats.derived._
import cats.implicits._
import ciris.Secret
import ciris.cats._
import enumeratum.EnumEntry
import eu.timepit.refined.auto._
import eu.timepit.refined.cats._
import eu.timepit.refined.types.net.UserPortNumber
import eu.timepit.refined.types.string.NonEmptyString
import is.cir.example.domain.config.AppEnvironment.{Local, Production, Testing}
import scala.concurrent.duration._

final case class Config(
  appName: NonEmptyString,
  environment: AppEnvironment,
  api: ApiConfig
)


object Config {

  import cats.implicits._

  implicit val showConfig: Show[Config] = {

    implicit val showInetAddress: Show[InetAddress] =
      Show.fromToString

    implicit def showEnumEntry[E <: EnumEntry]: Show[E] =
      Show.show(_.entryName)

    semi.show
  }
}

我必须在 build.sbt 中添加 scalac 选项 -Ypartial-unification 以解决添加后的另一个问题 我得到另一个之前没有发生的异常(没有标志)。

错误是-

[error] /Users/rajkumar.natarajan/Documents/Coding/OS/ciris-example/src/main/scala/is/cir/example/domain/config/Config.scala:38:10: type mismatch;
[error]  found   : cats.Show[shapeless.CNil]
[error]  required: cats.Show[is.cir.example.domain.config.Config]
[error]     semi.show
[error]          ^
[error] one error found

我该如何解决这个错误?

你应该调用它指定类型参数

semi.show[Config]

至少错误信息与那时不同

Error:(82, 14) diverging implicit expansion for type cats.derived.MkShow[is.cir.example.domain.config.Config]
starting with value tagRefType in object RefType
    semi.show[Config]

您可以尝试使用您的 Show 个实例。为了 Show 用于 "bigger" 案例 class 你应该有 Show 用于 "smaller" 案例。

implicitly[Show[Config]]
implicitly[Show[AppEnvironment]]
implicitly[Show[AppEnvironment.Local.type]]
//...
implicitly[Show[ApiConfig]]
implicitly[Show[InetAddress]]
//...