0

我正在尝试将一些参与者接收代码包装在 db 事务中,以构建通过更新数据库中的信息来处理事件的幂等持久视图。

class TrajectoryView extends IdempotentView {


  override def viewId: String = "trajectoryView"

  override def persistenceId: String = "sample-id-1"

  override def handleEvent(event: Event): Unit = {
      case ProcessDetectedEvent(time, processData, id, activityType) =>
        val coords = Db.save(processData.coordinates)
        Db.save(CoordinatesWrapper(id, coords))

      case ProcessUpdatedEvent(id, processData, time) =>
        val coords = Db.save(processData.coordinates)
        Db.save(CoordinatesWrapper(id, coords))
  }
}

abstract class IdempotentView extends PersistentView {

  def handleEvent(event: Event): Unit = ???
  var maxSeenSeqNumber = Db.query[SeqNumberWrapper].fetchOne().getOrElse(SeqNumberWrapper(0, viewId)).seqNumber

  def receive: Receive = {
    case event: Event =>
      if (lastSequenceNr > maxSeenSeqNumber) {
        Db.transaction {
          Db.save(SeqNumberWrapper(lastSequenceNr, viewId))
          handleEvent(event)
        }
      }

    case _: Unit =>
      println("Unknown message")
  }
}



Error:(22, 50) missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5) 

Expected type was: Unit   override def handleEvent(event: Event): Unit = { 

我应该怎么办?我不明白这个错误的确切含义。从我的角度来看,我已经编写了所有类型,编译器不需要推断任何东西。

4

1 回答 1

4

选择其中之一

override def handleEvent:  Event => Unit = {
...
}

或者

override def handleEvent(event: Event): Unit = event match {
...
}

如果更准确地说,大括号内的代码适合定义PartialFunction[T,Unit]可转换为Unit. 所以通常你的代码是正确的,但编译器无法推断类型T并抱怨它。但如果它可能导致一个函数返回另一个函数,而不是你所期望的。

于 2015-10-19T11:35:49.253 回答