我有以下代码。
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中添加-Ypartial-unification
scalac选项以解决另一个问题,添加后我得到了另一个先前未发生的异常(没有标志)。
错误是——
[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
,您应该为"较小"案例类Show
。
implicitly[Show[Config]]
implicitly[Show[AppEnvironment]]
implicitly[Show[AppEnvironment.Local.type]]
//...
implicitly[Show[ApiConfig]]
implicitly[Show[InetAddress]]
//...