cats-effect-resource

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cats Effect Resource (Scala)

Cats Effect Resource (Scala)

Quick start

快速开始

  • Model each resource with
    Resource.make
    or
    Resource.fromAutoCloseable
    and keep release idempotent.
  • Compose resources with
    flatMap
    ,
    mapN
    ,
    parMapN
    , or helper constructors; expose
    Resource[F, A]
    from APIs.
  • Use
    Resource
    at lifecycle boundaries and call
    .use
    only at the program edges.
  • Read
    references/resource.md
    for patterns, best practices, and API notes.
  • 使用
    Resource.make
    Resource.fromAutoCloseable
    为每个资源建模,并确保释放操作具有幂等性。
  • 使用
    flatMap
    mapN
    parMapN
    或辅助构造器组合资源;在API中对外暴露
    Resource[F, A]
    类型。
  • 在生命周期边界处使用
    Resource
    ,仅在程序的入口/出口边缘调用
    .use
    方法。
  • 阅读
    references/resource.md
    以了解相关模式、最佳实践及API说明。

Workflow

工作流程

  1. Identify acquisition, use, and release steps; decide if acquisition is blocking.
  2. Implement a
    Resource[F, A]
    constructor using the smallest needed typeclass.
  3. Compose resources into higher-level resources and keep finalizers minimal.
  4. Decide how cancelation and errors should influence release logic.
  5. Run with
    .use
    at the boundary (IOApp, service startup) and avoid leaking raw
    A
    .
  1. 明确资源的获取、使用与释放步骤;判断资源获取操作是否为阻塞型。
  2. 使用所需最小的类型类实现
    Resource[F, A]
    构造器。
  3. 将基础资源组合为更高层级的资源,并保持终结器逻辑尽可能简洁。
  4. 确定取消操作与错误应如何影响资源释放逻辑。
  5. 在边界处(如IOApp、服务启动阶段)通过
    .use
    运行资源,避免直接暴露原始的
    A
    类型实例。

Usage guidance

使用指南

  • Prefer
    Resource
    over
    try/finally
    or
    bracket
    when composition and cancelation safety matter.
  • Use
    IO.blocking
    (or
    Sync[F].blocking
    ) for acquisition and release when calling blocking JVM APIs.
  • For background fibers, use
    Resource
    or
    Supervisor
    to ensure cleanup on cancelation.
  • 当需要保障组合性与取消安全性时,优先使用
    Resource
    而非
    try/finally
    bracket
  • 调用阻塞式JVM API进行资源获取或释放时,使用
    IO.blocking
    (或
    Sync[F].blocking
    )。
  • 对于后台协程,使用
    Resource
    Supervisor
    确保在取消时能完成清理操作。

References

参考资料

  • Load
    references/resource.md
    for API details, patterns, and examples.
  • For Kotlin/Arrow parallels, see the
    arrow-resource
    skill.
  • Install this skill with
    npx skills add https://github.com/alexandru/skills --skill cats-effect-resource
    .
  • 加载
    references/resource.md
    查看API细节、模式及示例。
  • 若需了解Kotlin/Arrow的对应实现,可参考
    arrow-resource
    技能。
  • 通过
    npx skills add https://github.com/alexandru/skills --skill cats-effect-resource
    安装此技能。