cats-effect-resource
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCats Effect Resource (Scala)
Cats Effect Resource (Scala)
Quick start
快速开始
- Model each resource with or
Resource.makeand keep release idempotent.Resource.fromAutoCloseable - Compose resources with ,
flatMap,mapN, or helper constructors; exposeparMapNfrom APIs.Resource[F, A] - Use at lifecycle boundaries and call
Resourceonly at the program edges..use - Read for patterns, best practices, and API notes.
references/resource.md
- 使用或
Resource.make为每个资源建模,并确保释放操作具有幂等性。Resource.fromAutoCloseable - 使用、
flatMap、mapN或辅助构造器组合资源;在API中对外暴露parMapN类型。Resource[F, A] - 在生命周期边界处使用,仅在程序的入口/出口边缘调用
Resource方法。.use - 阅读以了解相关模式、最佳实践及API说明。
references/resource.md
Workflow
工作流程
- Identify acquisition, use, and release steps; decide if acquisition is blocking.
- Implement a constructor using the smallest needed typeclass.
Resource[F, A] - Compose resources into higher-level resources and keep finalizers minimal.
- Decide how cancelation and errors should influence release logic.
- Run with at the boundary (IOApp, service startup) and avoid leaking raw
.use.A
- 明确资源的获取、使用与释放步骤;判断资源获取操作是否为阻塞型。
- 使用所需最小的类型类实现构造器。
Resource[F, A] - 将基础资源组合为更高层级的资源,并保持终结器逻辑尽可能简洁。
- 确定取消操作与错误应如何影响资源释放逻辑。
- 在边界处(如IOApp、服务启动阶段)通过运行资源,避免直接暴露原始的
.use类型实例。A
Usage guidance
使用指南
- Prefer over
Resourceortry/finallywhen composition and cancelation safety matter.bracket - Use (or
IO.blocking) for acquisition and release when calling blocking JVM APIs.Sync[F].blocking - For background fibers, use or
Resourceto ensure cleanup on cancelation.Supervisor
- 当需要保障组合性与取消安全性时,优先使用而非
Resource或try/finally。bracket - 调用阻塞式JVM API进行资源获取或释放时,使用(或
IO.blocking)。Sync[F].blocking - 对于后台协程,使用或
Resource确保在取消时能完成清理操作。Supervisor
References
参考资料
- Load for API details, patterns, and examples.
references/resource.md - For Kotlin/Arrow parallels, see the skill.
arrow-resource - Install this skill with .
npx skills add https://github.com/alexandru/skills --skill cats-effect-resource
- 加载查看API细节、模式及示例。
references/resource.md - 若需了解Kotlin/Arrow的对应实现,可参考技能。
arrow-resource - 通过安装此技能。
npx skills add https://github.com/alexandru/skills --skill cats-effect-resource