AgentSkillsCN

arrow-resource

借助 `Resource`、`ResourceScope` 与 `resourceScope`,实现 Kotlin + Arrow 的资源生命周期管理。可用于设计文件、流、数据库池/连接、HTTP 客户端或分段上传等资源的安全获取与释放;组合各类资源(包括并行获取);或将 Resource 与类型化错误及取消机制无缝集成。

SKILL.md
--- frontmatter
name: arrow-resource
description: Kotlin + Arrow Resource lifecycle management with `Resource`, `ResourceScope`, and `resourceScope`. Use for designing safe acquisition/release of files, streams, DB pools/connections, HTTP clients, or multipart parts; composing resources (including parallel acquisition); or integrating Resource with typed errors and cancellation.

Arrow Resource (Kotlin)

Quick start

  • Use resourceScope { ... } at lifecycle boundaries.
  • Define each resource with resource { install(acquire) { a, exitCase -> release } }.
  • Compose with .bind(); use parZip for independent parallel acquisition.
  • Read references/resource.md for patterns and concrete examples.

Workflow

  1. Identify the acquire step and the release step.
  2. Implement a Resource<A> using install.
  3. Create reusable constructors on ResourceScope when needed.
  4. Compose resources into higher-level resources with .bind().
  5. Execute in resourceScope and keep finalizers idempotent.

Usage guidance

  • Prefer Resource over use/try/finally when you need suspend finalizers or MPP support.
  • Use ExitCase to handle rollback/cleanup differences.
  • When mixing with typed errors, pick nesting order deliberately to control ExitCase seen by finalizers.

References

  • Load references/resource.md for API details and end-to-end examples.