值 withFilter 不是 Cats IO in 的成员以供理解 [英] Value withFilter is not a member of Cats IO in for comprehension

查看:34
本文介绍了值 withFilter 不是 Cats IO in 的成员以供理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码,它编译得很好

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

我收到一个编译时错误

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)

为什么我不能用 IO 指定类型?

Why can't I specify the type with IO?

我启用了部分统一,所以不可能是这样:)

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

推荐答案

你的 for-comprehension 被脱糖形成,它使用函数 withFilter 并且因为 IO 没有有那个方法,编译失败.

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

幸运的是,有编译器插件 better-monadic-for,解决了这个问题.

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

只需在 build.sbt 中添加 addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.0") ,你应该没事.

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

这篇关于值 withFilter 不是 Cats IO in 的成员以供理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆