0

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

leozilla
  • 1,296
  • 2
  • 19
  • 29

1 Answers1

0

Try to use higher-kinded type parameter as in the definition of SubFlow

implicit class SideEffectfulSubFlowOps[+Out, +Mat, +FOps[+_], C](val enrichedSubFlow: SubFlow[Out, Mat, FOps, C]) extends AnyVal
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • I already tried that and it also does not work. Compiler error: `argument expression's type is not compatible with formal parameter type` – leozilla Oct 27 '21 at 05:38
  • @leozilla Can't reproduce. https://scastie.scala-lang.org/RlTubBvcTQadwh9S7dW7Kw Please provide the whole code that doesn't compile with this error. – Dmytro Mitin Oct 27 '21 at 06:59
  • if u change to scala 2.12.12 it does not compile anymore. https://scastie.scala-lang.org/IIeRtsG1SImlxacl2HzZIA – leozilla Oct 27 '21 at 09:14
  • @leozilla The problem seems to be that type parameterrs are not inferred. The first case (with explicit call) can be saved specified type parameters https://scastie.scala-lang.org/DmytroMitin/76yS2AG6SMGDrEZZDC9VdA The problem you can't specify them with extension method. I'm afraid not much can be done here in 2.12. – Dmytro Mitin Oct 27 '21 at 10:50