我有这样的特征定义:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
Run Code Online (Sandbox Code Playgroud)
我有这个特征的实现,如下所示:
object MyTrait {
def apply(system: ActorSystem, actorMaterializer: Materializer): MyTrait = new MyTrait {
override val appConfig: AppConfig = AppConfig.load()
// Get the ERROR here saying Value dbService overrides nothing
override val dbService: DBService[Task] = DBServiceT.asTask(appConfig.dbConfig)(scala.concurrent.ExecutionContext.Implicits.global)
override val supervisorActor: ActorRef =
system.actorOf(
SupervisorActor.props(appConfig)(monix.execution.Scheduler.Implicits.global),
s"${appConfig.appName}-supervisor"
)
}
}
Run Code Online (Sandbox Code Playgroud)
我的DBService特性如下所示:
trait DBService[M[_]] {
def allPowerPlants(fetchOnlyActive: Boolean): M[Seq[PowerPlantRow]]
def powerPlantsPaginated(filter: PowerPlantFilter): M[Seq[PowerPlantRow]]
def allPowerPlantsPaginated(fetchOnlyActive: Boolean, pageNumber: Int): M[Seq[PowerPlantRow]]
def powerPlantById(id: Int): M[Option[PowerPlantRow]]
def newPowerPlant(powerPlantRow: PowerPlantRow): M[Int]
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个看起来像这样的实现:
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我的MyTrait对象中出现一个错误:
Value dbService overrides nothing
Run Code Online (Sandbox Code Playgroud)
我有什么想法我做错了吗?
这个签名:
def dbService[M[_]]: DBService[M[_]]
描述了可以为任何类型的构造函数创建DBService的东西M[_].为了它的类型检查它必须能够创建所有:
DBService[Task]DBService[Future]DBService[Array]DBService[Option]DBService[Ordering]由于您的实现只能为单个 M[_](Task在您的情况下)生成一个值,因此您无法拥有该签名.
您的选项包括将类型参数移动到特征定义,作为类型参数:
trait MyTrait[M[_]] {
def dbService: DBService[M] // also note that [_] part should not be here
}
Run Code Online (Sandbox Code Playgroud)
或类型成员:
trait MyTrait {
type M[_]
def dbService: DBService[M]
}
Run Code Online (Sandbox Code Playgroud)
然而,后者可能会令人讨厌
编辑:你当然也可以选择Task直接指定:
trait MyTrait {
def dbService: DBService[Task]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
235 次 |
| 最近记录: |