Search code examples
scalaevalscala-catsfs2

fs2 Stream scala No implicit of type : Stream.Compiler[Eval,G_]


I'm trying to create a Stream[Eval, String] as follows :

import cats.Eval
import cats.effect.{ExitCode, IO, IOApp}
import fs2._

object StringEval extends IOApp {

  def evalString: Eval[String] = Eval.always{
      val r = new scala.util.Random(31)
      r.nextString(4)
    }

  override def run(args: List[String]): IO[ExitCode] = {

    Stream
      .eval(evalString)
      .repeat
      .compile
      .lastOrError
      .start
      .as(ExitCode.Success)

  }
}

But the problem is that I'm getting a compilation error saying :

Error:(17, 8) could not find implicit value for parameter compiler: fs2.Stream.Compiler[[x]cats.Eval[x],G]
      .compile

I can't seem to get the error ? what am I missing? What does the error refer to ?


Solution

  • Fs2 Stream.Compiler is not found (could not find implicit value Compiler[[x]F[x],G])

    Fs2 Stream#compile now requires a Sync[F]

    Eval doesn't have instance of Sync, IO does.

    Try

    def evalString: IO[String] = {
      val r = new scala.util.Random(31)
      Sync[IO].delay(r.nextString(4))
    }
    
    Stream
      .eval(evalString)
      .repeat
      .compile
      .lastOrError
      .start
      .as(ExitCode.Success)