通用参数scala的访问类型参数

时间:2017-11-06 14:31:37

标签: scala generics akka-stream scala-generics

我正在尝试在Akka流中创建以下函数的包装器。

RestartFlow.withBackoff(minBackoff = 3.seconds,
      maxBackoff = 30.seconds,
      randomFactor = 0.2) {
      () => s
    }

其中s是我用后退包装的一些来源。理想情况下ID就像这样

RetryFlow(s)

我设法创造了这个:

object RetryFlow {
  def apply[In, Out, _, T <: Flow[In, Out, _]](source: T, minBackoff: FiniteDuration = 3.seconds, maxBackoff: FiniteDuration = 30.seconds, randomFactor: Double = 0.2): Flow[In, Out, NotUsed] = {
    RestartFlow.withBackoff(
      minBackoff = minBackoff,
      maxBackoff = maxBackoff,
      randomFactor = randomFactor) {
      () => source
    }
  }
}

问题是我需要在呼叫站点再次提供流量的所有3种类型的参数并且它看起来很可怕

RetryFlow[JustDataEvent, JustDataEvent, NotUsed, Flow[JustDataEvent, JustDataEvent, NotUsed]](s)

它也不是类型安全的,因为我可以在这里键入任何类型的参数。

我觉得应该是可能的,但是我不知道如何不进入额外的类型参与进出,而是做一些像T#In,T#Out等等,因为我已经说T延伸了流量,因此,T已经有我需要的类型参数。

1 个答案:

答案 0 :(得分:2)

如果只使用Flow[In, Out, _]类型的参数,而不是object RetryFlow { def apply[In, Out, _]( source: Flow[In, Out, _], minBackoff: FiniteDuration = 3.seconds, maxBackoff: FiniteDuration = 30.seconds, randomFactor: Double = 0.2): Flow[In, Out, NotUsed] = { RestartFlow.withBackoff[In, Out](minBackoff = minBackoff, maxBackoff = maxBackoff, randomFactor = randomFactor) { () => source } } } 怎么样?

val value: Flow[Int, String, NotUsed] = Flow.fromFunction[Int, String](i => i.toString)
RetryFlow(value)

然后我们得到:

SELECT first_name, second_name, attribute
FROM users
WHERE attribute
REGEXP BINARY concat('%', first_name, '_', second_name, '%');