Scala从特征重写泛型方法
我有这样一个特质定义:Scala从特征重写泛型方法,scala,Scala,我有这样一个特质定义: trait MyTrait { def dbService[M[_]]: DBService[M[_]] def appConfig: AppConfig def supervisorActor: ActorRef } object MyTrait { def apply(system: ActorSystem, actorMaterializer: Materializer): MyTrait = new MyTrait {
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
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"
)
}
}
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]
}
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
trait MyTrait[M[_]] {
def dbService: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
我实现了这样一种特性:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
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"
)
}
}
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]
}
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
trait MyTrait[M[_]] {
def dbService: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
我的DBService特性如下所示:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
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"
)
}
}
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]
}
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
trait MyTrait[M[_]] {
def dbService: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
然后,我有一个如下所示的实现:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
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"
)
}
}
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]
}
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
trait MyTrait[M[_]] {
def dbService: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
当我尝试此操作时,我在MyTrait对象中得到一个错误,该错误表示:
Value dbService overrides nothing
你知道我做错了什么吗?这个签名:
def dbService[M[\u]]:dbService[M[\u]]
描述可以为任何类型构造函数M[\u]
创建DBService的内容。要进行类型检查,它必须能够创建以下内容的所有内容:
DBService[Task]
DBService[未来]
DBService[Array]
DBService[选项]
DBService[订购]
- ……等等
M[\u]
(Task
)生成一个值,因此您不能拥有该签名
您的选项包括将类型参数作为类型参数移动到特征定义:
trait MyTrait[M[_]] {
def dbService: DBService[M] // also note that [_] part should not be here
}
或类型成员:
trait MyTrait {
type M[_]
def dbService: DBService[M]
}
然而,使用后者可能会造成麻烦
编辑:您还可以选择直接指定任务
,当然:
trait MyTrait {
def dbService: DBService[Task]
}
此签名:
def dbService[M[\u]]:dbService[M[\u]]
描述可以为任何类型构造函数M[\u]
创建DBService的内容。要进行类型检查,它必须能够创建以下内容的所有内容:
DBService[Task]
DBService[未来]
DBService[Array]
DBService[选项]
DBService[订购]
- ……等等
M[\u]
(Task
)生成一个值,因此您不能拥有该签名
您的选项包括将类型参数作为类型参数移动到特征定义:
trait MyTrait[M[_]] {
def dbService: DBService[M] // also note that [_] part should not be here
}
或类型成员:
trait MyTrait {
type M[_]
def dbService: DBService[M]
}
然而,使用后者可能会造成麻烦
编辑:您还可以选择直接指定任务
,当然:
trait MyTrait {
def dbService: DBService[Task]
}
val-dbService:dbService[Task]
是一个类型为dbService[Task]
的值,而您需要定义一个将类型作为“参数”的函数:def-dbService[M[\u]]:dbService[M[\u]]
我想你希望你的特质是这样的:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
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"
)
}
}
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]
}
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
trait MyTrait[M[_]] {
def dbService: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
val-dbService:dbService[Task]
是一个类型为dbService[Task]
的值,而您需要定义一个将类型作为“参数”的函数:def-dbService[M[\u]]:dbService[M[\u]]
我想你希望你的特质是这样的:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
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"
)
}
}
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]
}
class DBServiceTask private (dbConfig: DBConfig)(implicit ec: ExecutionContext) extends DBService[Task] { self =>
....
....
}
trait MyTrait[M[_]] {
def dbService: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
忘记了对象MyTrait扩展MyTrait可能?不!这不是问题!我已经编辑了我的帖子!现在看看!在
trait
中尝试typem[\u]
,然后在您的实现中将其修复为Task
。我不想用类型定义污染trait!还有其他可能吗?忘记对象MyTrait扩展MyTrait吗?没有!这不是问题!我已经编辑了我的帖子!现在看看!在trait
中尝试typem[\u]
,然后在您的实现中将其修复为Task
。我不想用类型定义污染trait!还有其他可能性吗?