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 :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With