scala - 我怎么知道,需要哪个隐含的?
问题描述
我有一个工作代码:
final case class Services[F[_]](c: Client[F], fooUrl: String)
(implicit cf: ConcurrentEffect[F]) {
private val dsl = Http4sDsl[F]
import dsl._
def routes: HttpRoutes[F] = HttpRoutes.of[F] {
case GET -> Root / "call" =>
c.get(fooUrl) { res =>
Applicative[F].pure {
Response[F]().withEntity {
res
.body
.through(text.utf8Decode)
.map(msg => "Forwarded through Boo" |+| msg)
}
}
}
}
当我删除隐式 importimplicit cf: ConcurrentEffect[F]
时,编译器会抱怨:
[error] /home/developer/scala/boo-service/src/main/scala/com/sweetsoft/Services.scala:29:20: could not find implicit value for parameter instance: cats.Applicative[F]
[error] Applicative[F].pure {
[error] ^
[error] /home/developer/scala/boo-service/src/main/scala/com/sweetsoft/Services.scala:26:48: could not find implicit value for evidence parameter of type cats.Defer[F]
[error] def routes: HttpRoutes[F] = HttpRoutes.of[F] {
[error] ^
[error] two errors found
我怎么知道,implicit cf: ConcurrentEffect[F]
缺少类型类?
解决方案
我认为如果您查看猫效应库的来源,您可以获得答案:
让我们看看 ConcurrentEffect 定义:
trait ConcurrentEffect[F[_]] extends Concurrent[F] with Effect[F] {...
嗯,现在让我们看看并发:
trait Concurrent[F[_]] extends Async[F] {...
异步:
trait Async[F[_]] extends Sync[F] with LiftIO[F] {...
下一个:
trait Sync[F[_]] extends Bracket[F, Throwable] with Defer[F] {
现在我们可以得出结论,ConcurrentEffect
扩展Defer
同样的尝试对Applicative
自己做;)
要快速处理此类错误-我认为您必须了解扩展层次结构-以便您可以快速找出所需的类型类
推荐阅读
- discord.js - discord.js 不在特定时间发送消息
- python - 在树莓派上使用 python 的 Web 服务器,无法从 LAN 或 Internet 访问
- php - 致命错误:未捕获的错误:找不到类“todo\views\DataBase”
- reactjs - Apollo useQuery 或 client.query React 无法记录值
- visual-studio-code - 我使用 Yeoman 创建了一个 VS Code 扩展。我使用的是哪个版本的 Electron?
- java - statement.executeQuery () 不起作用
- jquery - jQuery:特殊情况下窗口大小更改的更新功能
- wordpress - 如何在 wordpress 中实现 Jcrop 脚本
- python - 如何将熊猫数据框中不同值的数量作为整数返回
- javascript - 如何在 addEventListener 跨浏览器中使用 e.path?