typelevel / typelevel/fs2

Type inference failing 3.2.8

Open
#2,927 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Scala
Stars
2.5k
Forks
636
Avg merge
2d 4h
Merged PRs (30d)
7

Description

Before 3.2.8 the following used to compile as is:

 /** Buffer until `f` returns true
    * @note
    *   Careful on unbounded memory growth
    */
extension [F[_]: Applicative, A](src: fs2.Stream[F, A])
  def bufferUntil(f: A => Boolean): Stream[F, Chunk[A]] = {
    def loop(s: Stream[F, (A, Boolean)], acc: Chunk[A]): Pull[F, Chunk[A], Unit] =
      s.pull.uncons1.flatMap {
        case Some(((a, true), tl))  => Pull.output1(acc ++ Chunk.singleton(a)) >> loop(tl, Chunk.empty)
        case Some(((a, false), tl)) => loop(tl, acc ++ Chunk.singleton(a))
        case None                   => Pull.output1(acc).whenA(acc.nonEmpty)
      }
    loop(src.map(x => x -> f(x)), Chunk.empty).stream
  }

Scastie link: https://scastie.scala-lang.org/4JW4H1XbQHWYh8BjScmfxw

The compiler error:

Could not find an instance of Applicative for ([R] =>> fs2.Pull[Nothing, fs2.Chunk[A], R]).
I found:

    cats.Invariant.catsApplicativeForArrow[([O, R] =>> fs2.Pull[Nothing, O, R]), 
      fs2.Chunk[A]
    ](/* missing */summon[cats.arrow.Arrow[[O, R] =>> fs2.Pull[Nothing, O, R]]])

But no implicit values were found that match type cats.arrow.Arrow[[O, R] =>> fs2.Pull[Nothing, O, R]].

One of the following imports might make progress towards fixing the problem:

  import fs2.Compiler.Target.forConcurrent
  import fs2.Compiler.Target.forSync

This can now be fixed by manually specifying the types by changing the last line to:

case None                   => Pull.output1[F, Chunk[A]](acc).whenA(acc.nonEmpty)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Scastie reproduction linked in the issue and compare compilation before and after 3.2.8. Investigate the final Pull.output1 call and the reported Applicative/type-inference error; done means the original code compiles without manually specifying F and Chunk[A].

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
stream-processing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.