[英]Scala Override Generic Method from a Trait
I have a trait definition like this: 我有一个这样的特征定义:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
I have an implementation of this trait that looks like this: 我有一个如下特征的实现:
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"
)
}
}
My DBService trait looks like this: 我的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]
}
I then have an implementation that looks like this: 然后,我有一个类似于以下的实现:
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
When I tried this, I get an error in my MyTrait object that says: 尝试此操作时,我的MyTrait对象出现错误,提示:
Value dbService overrides nothing
Any ideas what I'm doing wrong? 有什么想法我做错了吗?
This signature: 此签名:
def dbService[M[_]]: DBService[M[_]]
describes something that can create a DBService for ANY type constructor M[_]
. 描述了可以为ANY类型构造函数
M[_]
创建DBService的内容。 For it to typecheck it must be able to create all of: 要对其进行类型检查,它必须能够创建所有以下内容:
DBService[Task]
DBService[Future]
DBService[Array]
DBService[Option]
DBService[Ordering]
Since your implementation can only produce a value for a single M[_]
( Task
in your case), you cannot have that signature. 由于您的实现只能产生单个
M[_]
(在您的情况下为Task
),因此您不能具有该签名。
Your options include moving a type parameter to a trait definition, either as a type parameter: 您的选择包括将类型参数作为类型参数移动到特征定义:
trait MyTrait[M[_]] {
def dbService: DBService[M] // also note that [_] part should not be here
}
or a type member: 或类型成员:
trait MyTrait {
type M[_]
def dbService: DBService[M]
}
The latter, however, might be a nuisance to use 但是,后者可能会给您带来麻烦
EDIT : You also have the option of specifying Task
directly, of course: 编辑 :当然,您也可以选择直接指定
Task
:
trait MyTrait {
def dbService: DBService[Task]
}
val dbService: DBService[Task]
is a value of type DBService[Task]
, whereas you need to define a function that takes a type as "parameter": def dbService[M[_]]: DBService[M[_]]
val dbService: DBService[Task]
是类型DBService[Task]
的值,而您需要定义一个将类型作为“参数”的函数: def dbService[M[_]]: DBService[M[_]]
I think you want your trait to look like this: 我认为您希望自己的特征看起来像这样:
trait MyTrait[M[_]] {
def dbService: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.