私は Scala とdoobieを使ったプロジェクトに取り組んでいます。MacWireによって注入され、さまざまな Task/Future モナド (テストなど) で使用される可能性のあるトランザクション特性を実装しようとしています。
import doobie.imports
import doobie.imports._
import fs2.Task
import fs2.util.{Catchable, Suspendable}
trait Transactor[M[_]] {
implicit def catchable: Catchable[M] = implicitly
val transactor: M[imports.Transactor[M]]
def transact[A](q: ConnectionIO[A]): M[A] = catchable.flatMap(transactor)(xa => q.transact(xa))
}
case class H2Transactor[M[_]: Catchable: Suspendable](pure: PureConfig) extends Transactor[M] {
override val transactor = doobie.h2.imports.H2Transactor[M](
pure.config.persistence.name,
pure.config.persistence.user.orNull,
pure.config.persistence.password.orNull
)
}
...
trait DatabaseModule { lazy val transactor = wire[H2Transactor[Task]] }
trait TestModule { lazy val transactor = wire[H2Transactor[IOLite]] }
しかし、私はこのエラーが発生します:
[error] .../src/main/scala/.../persistence/transactor/H2Transactor.scala:13: type mismatch;
[error] found : M[doobie.h2.h2transactor.H2Transactor[M]]
[error] required: M[doobie.imports.Transactor[M]]
[error] (which expands to) M[doobie.util.transactor.Transactor[M]]
[error] Note: doobie.h2.h2transactor.H2Transactor[M] <: doobie.imports.Transactor[M], but type M is invariant in type _.
[error] You may wish to define _ as +_ instead. (SLS 4.5)
Task
として定義されているため機能しますTask[+A]
が、ドゥービーはIOLite
として定義されていIOLite[A]
ます。これを機能させる方法はありますか?または、代わりに別のモナドタイプを使用する必要がありますか (doobie テストにはTask
変更できません)。IOLite