I am having a hard time to get an implicit class
for an akka.stream.scaladsl.SubFlow
to compile.
My test code:
val subFlow = Source(List("1", "2", "3"))
.groupBy(1, f)
val richSubFlow = new SideEffectfulSubFlowOps(subFlow)
val got = richSubFlow
.withSideEffect((elem: String) => recordedItems.add(elem))
.mergeSubstreams
.to(Sink.seq)
/* In the end I would like to write it like this:
val got = Source(List("1", "2", "3"))
.groupBy(1, f)
.withSideEffect((elem: String) => recordedItems.add(elem))
.mergeSubstreams
.to(Sink.seq)
*/
The implicit class I have so far.
implicit class SideEffectfulSubFlowOps[+Out, +Mat, FOps <: FlowOps[Out, Mat], C](val enrichedSubFlow: SubFlow[Out, Mat, FOps#Repr, C]) extends AnyVal {
/** Perform a side effect without mutating the stream's element.
* Unlike [[SubFlow.alsoTo]] and [[SubFlow.wireTap]], this operation has the same semantics as [[SubFlow.map]] regarding backpressure, and concurrency */
def withSideEffect(f: Out => Unit): enrichedSubFlow.Repr[Out] = {
enrichedSubFlow.map { o =>
f(o)
o
}
}
}
Unfortunately I cannot figure out the proper generic types to define for the implicit class.
The compiler error:
[error] SubFlowExtensionsSpec.scala:21:43: type mismatch;
[error] found : akka.stream.scaladsl.SubFlow[String,akka.NotUsed,[+O]akka.stream.scaladsl.Source[O,akka.NotUsed],akka.stream.scaladsl.RunnableGraph[akka.NotUsed]]
[error] required: akka.stream.scaladsl.SubFlow[?,?,?#Repr,?]
[error] val x = new SideEffectfulSubFlowOps(subFlow)
Looking at the definition of subflow: trait SubFlow[+Out, +Mat, +F[+_], C] extends FlowOps[Out, Mat]
I dont understand how I need to define the generic types on my implicit class which are then used for type F
and C
of the SubFlow
.
Scala Version: 2.12.12