我的流程看起来像这样:
case class T1 extends A
case class T2 extends A
case class T3 extends A
val proc: Flow[A, B, Unit] = Flow[A] .map {
case x: T1 => B()
case x: T2 => B()
case x: T3 =>
save_message(x)
// now throw away the message
}
.transform(() => lastStage)
val lastStage = ...
def save_message(msg: A): Unit = ...
所以在这段代码中,我收到一个基类型为A的消息,如果它是派生类型T1或T2的话。但是,如果它是T3类型,我只想把它丢弃而不将它传递给lastStage,只需继续下一条消息即可。我该怎么做?
答案 0 :(得分:4)
使用collect
代替map
:
case class T1 extends A
case class T2 extends A
case class T3 extends A
val proc: Flow[A, B, Unit] = Flow[A].collect {
case x: T1 => B()
case x: T2 => B()
}
.transform(() => lastStage)
val lastStage = ...