Scala:返回依赖类型
Scala: Returning a dependent type
在我的应用程序中,我有一组封闭的操作,return 对应的一组响应如下所示。
sealed trait OperationCompletionResponse {
val state: Int
}
case class ExecutionStartedResponse(state: Int) extends OperationCompletionResponse
case class UpdateRecordedResponse(state: Int) extends OperationCompletionResponse
case class ExecutionTerminatedResponse(state: Int) extends OperationCompletionResponse
sealed trait Operation {
type R
def createResponse(state: Int): R
}
case class StartExecutionOperation() extends Operation {
type R = ExecutionStartedResponse
override def createResponse(state: Int): ExecutionStartedResponse = ExecutionStartedResponse(state)
}
case class RecordUpdateOperation() extends Operation {
type R = UpdateRecordedResponse
override def createResponse(state: Int): UpdateRecordedResponse = UpdateRecordedResponse(state)
}
case class TerminateExecutionOperation() extends Operation {
type R = ExecutionTerminatedResponse
override def createResponse(state: Int): ExecutionTerminatedResponse = ExecutionTerminatedResponse(state)
}
就我对类型成员和类型投影的理解而言,我可以执行以下操作。根据 scala 编译器
,它们是完全有效的语句
val esr:StartExecutionOperation#R = ExecutionStartedResponse(1)
val teo:TerminateExecutionOperation#R = ExecutionTerminatedResponse(-1)
val ruo:RecordUpdateOperation#R = UpdateRecordedResponse(0)
但是,我现在想在函数中使用它们;这通常更有用。现在,如何将输出类型指定为依赖类型?
def updateState[O <: Operation](operation: O) = operation match {
case StartExecutionOperation() => ExecutionStartedResponse(1)
case TerminateExecutionOperation() => ExecutionTerminatedResponse(-1)
case RecordUpdateOperation() => UpdateRecordedResponse(0)
}
更具体地说,在我的例子中,我不希望函数的 return 类型是 OperationCompletionResponse,而是类似于 Operation#R 或 operation.R
我怎样才能做到这一点?
updateState
的路径相关类型将直接链接到 operation
的类型。你不想在正文中匹配 operation
,因为这永远不会给你 R
你正在寻找的类型。
您恰好定义了一个操作,它为您提供了这个 R
,那就是 createResponse
。因为 createResponse
需要一个整数参数,所以你必须以某种方式在 updateState
中给出它。似乎每个操作都有一些默认状态,因此您可以定义 def defaultState: Int
Operation
然后
def updateState(op: Operation): op.R = op.createResponse(op.defaultState)`
如果这不能回答您的问题,请对其进行编辑以更具体地说明您在此状态下想要实现的目标。
在我的应用程序中,我有一组封闭的操作,return 对应的一组响应如下所示。
sealed trait OperationCompletionResponse {
val state: Int
}
case class ExecutionStartedResponse(state: Int) extends OperationCompletionResponse
case class UpdateRecordedResponse(state: Int) extends OperationCompletionResponse
case class ExecutionTerminatedResponse(state: Int) extends OperationCompletionResponse
sealed trait Operation {
type R
def createResponse(state: Int): R
}
case class StartExecutionOperation() extends Operation {
type R = ExecutionStartedResponse
override def createResponse(state: Int): ExecutionStartedResponse = ExecutionStartedResponse(state)
}
case class RecordUpdateOperation() extends Operation {
type R = UpdateRecordedResponse
override def createResponse(state: Int): UpdateRecordedResponse = UpdateRecordedResponse(state)
}
case class TerminateExecutionOperation() extends Operation {
type R = ExecutionTerminatedResponse
override def createResponse(state: Int): ExecutionTerminatedResponse = ExecutionTerminatedResponse(state)
}
就我对类型成员和类型投影的理解而言,我可以执行以下操作。根据 scala 编译器
,它们是完全有效的语句val esr:StartExecutionOperation#R = ExecutionStartedResponse(1)
val teo:TerminateExecutionOperation#R = ExecutionTerminatedResponse(-1)
val ruo:RecordUpdateOperation#R = UpdateRecordedResponse(0)
但是,我现在想在函数中使用它们;这通常更有用。现在,如何将输出类型指定为依赖类型?
def updateState[O <: Operation](operation: O) = operation match {
case StartExecutionOperation() => ExecutionStartedResponse(1)
case TerminateExecutionOperation() => ExecutionTerminatedResponse(-1)
case RecordUpdateOperation() => UpdateRecordedResponse(0)
}
更具体地说,在我的例子中,我不希望函数的 return 类型是 OperationCompletionResponse,而是类似于 Operation#R 或 operation.R
我怎样才能做到这一点?
updateState
的路径相关类型将直接链接到 operation
的类型。你不想在正文中匹配 operation
,因为这永远不会给你 R
你正在寻找的类型。
您恰好定义了一个操作,它为您提供了这个 R
,那就是 createResponse
。因为 createResponse
需要一个整数参数,所以你必须以某种方式在 updateState
中给出它。似乎每个操作都有一些默认状态,因此您可以定义 def defaultState: Int
Operation
然后
def updateState(op: Operation): op.R = op.createResponse(op.defaultState)`
如果这不能回答您的问题,请对其进行编辑以更具体地说明您在此状态下想要实现的目标。