Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value withFilter is not a member of Cats IO in for comprehension

I wrote this code and it compiles fine

for {
  list : List[Int] <- Future(List(1, 2, 3))
} yield list.size 

res7: Future[Int] = Future(Success(3))

But if I convert this code to

for {
  list : List[Int] <- IO(List(1, 2, 3))
} yield list.size

I get a compile time error

value withFilter is not a member of cats.effect.IO[List[Int]]

If I remove the type then it compiles fine

for {
  list  <- IO(List(1, 2, 3)) // returns IO[List[Int]]
} yield list.size 
res8: IO[Int] = Map(Delay(<function0>), <function1>, 0)

Why can't I specify the type with IO?

I have partial unification enabled so it can't be that :)

like image 344
Knows Not Much Avatar asked Apr 16 '19 19:04

Knows Not Much


1 Answers

Your for-comprehension gets desugared to form, which uses function withFilter and because IO doesn't have that method, compilation fails.

Fortunately, there is the compiler plugin better-monadic-for, which solves that problem.

Just add addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.0") in your build.sbt and you should be fine.

like image 55
Krzysztof Atłasik Avatar answered Oct 01 '22 13:10

Krzysztof Atłasik