2017-08-31 20 views
0

我對斯卡拉相對較新,我遇到了泛型類型參數的問題。我執行某些操作的命令模式,我有一個基類是這樣的:斯卡拉通用類型參數問題

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在存儲庫中,返回一個未來[序號[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){ 

爲什麼它說,它沒有類型參數,然後再次,希望之一。我不明白。請幫忙!

在此先感謝!

回答

1

的問題是在這裏:

case class AgentExecutionListResponse[Seq[AgentExecution]](response: Seq[AgentExecution]) extends CommandResponseWrapper 

[...]啄應該是CommandResponseWrapper後不後的類名

case class AgentExecutionListResponse(
    response: Seq[AgentExecution] 
) extends CommandResponseWrapper[Seq[AgentExecution]] 
0

對於第二個錯誤:

T[X] <: CommandResponseWrapper[X] forSome { type X }意味着你告訴編譯器T必須是一個類型構造帶一個參數(這就是爲什麼你可以寫T[X]execute的簽名),但AgentExecutionListResponse[Seq[AgentExecution]]用不了參數(你不能寫AgentExecutionListResponse[Seq[AgentExecution]][SomeType])。所以你不能用它作爲T。就好像您嘗試撥打foo(),其中foo有一個參數。

此外,我認爲forSomeX的範圍只是CommandResponseWrapper[X] forSome { type X },所以它與T[X] <: CommandResponseWrapper[_]相同。因此命名爲XT[X]令人困惑。

給你期待AgentExecutionListCommand#execute簽名,修復可能只是

abstract class Command[A, B <: BaseModel, T <: CommandResponseWrapper[_]](repository: BaseRepository[A, B], entity: B) { 

    @throws(classOf[Exception]) 
    def execute: Future[T] 
} 

,但它實際上取決於你想要什麼。

對於第一個,目前還不清楚S_來自哪裏:也許從getAllMastersForAgent的簽名?在這種情況下,答案是必要的。但第二個是更基本的問題。

+0

感謝您的答覆@Alexey羅曼諾夫。我在這裏寫的是AgentExecutionListCommand#execute必須返回一個CommandResponseWrapper的子類型,它也有一些嵌套類型(CommandResponseWrapper [可以是任何類型])。 –

+0

@JorgeCespedes這似乎正是我在「修復可能只是」段落中所建議的。嘗試一下,看看它是否成功。 –

+0

(儘管我沒有注意到另一個問題,迪瑪指出的那個問題) –