我有以下一段代码,它创建一个输出MarketFactors
的源。但是,我希望能够切换随机生成器的类型或toMarketFactors
函数的功能。
然而,由于初始源上的所有转换都在类型中,我不能将其转换为类实现(Option[Source[MarketFactors, NotUsed]]
)的特征中的泛型类型,我需要做什么?
我附加的Sink
和物化以后,这是一个不好的做法吗?
override def factors: Option[Source[CorrelatedRandomVectorGenerator, NotUsed]#Repr[MarketFactors]] = {
val source = for { g <- generator } yield Source.repeat(g)
val marketFactors = Flow[CorrelatedRandomVectorGenerator].map(toMarketFactors)
source.map(_.via(marketFactors))
}
在factors
方法上使用类型参数
case class CorrelatedRandomVectorGenerator()
case class MarketFactors1()
case class MarketFactors2()
def factors[T](transformation: CorrelatedRandomVectorGenerator => T): Option[Source[T, NotUsed]] = {
val generator = Option.empty[CorrelatedRandomVectorGenerator]
val source = for { g <- generator } yield Source.repeat(g)
val marketFactors = Flow[CorrelatedRandomVectorGenerator].map(transformation)
source.map(_.via(marketFactors))
}
val toMarketFactors1: CorrelatedRandomVectorGenerator => MarketFactors1 = ???
val toMarketFactors2: CorrelatedRandomVectorGenerator => MarketFactors2 = ???
val source1: Option[Source[MarketFactors1, NotUsed]] = factors(toMarketFactors1)
val source2: Option[Source[MarketFactors2, NotUsed]] = factors(toMarketFactors2)