Skip to contents

Decorator form of a contextmanager(): the decorated function runs inside the context, and cleanup runs when it returns or errors. As in Python's ContextDecorator, the context's value is not passed to the function.

Usage

as_decorator(ctx, ...)

Arguments

ctx

A context manager.

...

Arguments for ctx.

Value

A decorator.

Examples

local_tempdir := contextmanager %@% function() {
  path <- tempfile()
  dir.create(path)
  teardown(unlink(path, recursive = TRUE))
  path
}
g := as_decorator(local_tempdir) %@% function() "ran inside context"
g()
#> [1] "ran inside context"