Cats Effect Resource (Scala)
Quick start
- •Model each resource with
Resource.makeorResource.fromAutoCloseableand keep release idempotent. - •Compose resources with
flatMap,mapN,parMapN, or helper constructors; exposeResource[F, A]from APIs. - •Use
Resourceat lifecycle boundaries and call.useonly at the program edges. - •Read
references/resource.mdfor patterns, best practices, and API notes.
Workflow
- •Identify acquisition, use, and release steps; decide if acquisition is blocking.
- •Implement a
Resource[F, A]constructor using the smallest needed typeclass. - •Compose resources into higher-level resources and keep finalizers minimal.
- •Decide how cancelation and errors should influence release logic.
- •Run with
.useat the boundary (IOApp, service startup) and avoid leaking rawA.
Usage guidance
- •Prefer
Resourceovertry/finallyorbracketwhen composition and cancelation safety matter. - •Use
IO.blocking(orSync[F].blocking) for acquisition and release when calling blocking JVM APIs. - •For background fibers, use
ResourceorSupervisorto ensure cleanup on cancelation.
References
- •Load
references/resource.mdfor API details, patterns, and examples. - •For Kotlin/Arrow parallels, see the
arrow-resourceskill. - •Install this skill with
npx skills add https://github.com/alexandru/skills --skill cats-effect-resource.