【发布时间】:2017-08-31 21:33:52
【问题描述】:
我对 Scala 比较陌生,并且遇到了泛型类型参数的问题。我正在为某些操作实现命令模式,并且我有一个这样的基类:
abstract class Command[A, B <: BaseModel, T[X] <: CommandResponseWrapper[X] forSome { type X }](repository: BaseRepository[A, B], entity: B) {
@throws(classOf[Exception])
def execute: Future[T[X] forSome { type X }]
}
现在,将这个具体命令作为我遇到的问题的一个示例:
case class AgentExecutionListCommand(repository: AgentExecutionRepository[Int, AgentExecution], entity: AgentExecution)(implicit ec: ExecutionContext) extends Command[Int, AgentExecution, AgentExecutionListResponse[Seq[AgentExecution]]](repository, entity){
override def execute: Future[AgentExecutionListResponse[Seq[AgentExecution]]] = {
repository.getAllMastersForAgent(entity.agentId).map(ae => AgentExecutionListResponse(ae))
}
override def toString: String = "Command is: AgentExecutionListCommand"
}
case class AgentExecutionListResponse[Seq[AgentExecution]](response: Seq[AgentExecution]) extends CommandResponseWrapper
存储库中的方法getAllMastersForAgent,返回一个Future[Seq[AgentExecution]],但是编译器在这一行显示错误:
repository.getAllMastersForAgent(entity.agentId).map(ae => AgentExecutionListResponse(ae))
错误是:AgentExecutionListResponse[Seq] 类型的表达式不符合预期的类型 S_
这是什么意思?
另一个错误是:
Error:(11, 189) org.jc.dpwmanager.commands.AgentExecutionListResponse[Seq[org.jc.dpwmanager.models.AgentExecution]] takes no type parameters, expected: one
case class AgentExecutionListCommand(repository: AgentExecutionRepository[Int, AgentExecution], entity: AgentExecution)(implicit ec: ExecutionContext) extends Command[Int, AgentExecution, AgentExecutionListResponse[Seq]](repository, entity){
为什么它说它不接受类型参数,然后又需要一个。我不明白。请帮忙!
提前致谢!
【问题讨论】: