From hb at biostat.ucsf.edu  Sun Nov  3 22:42:08 2013
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Sun, 3 Nov 2013 13:42:08 -0800
Subject: [Rd] WISHLIST: on.exit(..., add=TRUE,
	where="first") to address common use cases
Message-ID: <CAFDcVCQ1Hq63=7tXuJRRC4zQJ9hB0qg+3pzzgKhq2_CGw5SRSw@mail.gmail.com>

Before trying to submit a patch(*) to on.exit(), I'd like to check
whether there is an interest in enhancing on.exit(..., add=TRUE) such
that it is possible to specify whether the added expression should be
added before or after already recorded expression.  The default is now
to add it after, but it would often be useful to add it before
previously recorded expressions.

EXAMPLE:

foo <- function(path="work") {
  # Change working directory. Make sure to reset on exit.
  opwd <- setwd(path)
  on.exit(setwd(opwd))

  # Write to a local temporary file.  Make sure to remove it on exit.
  cat("Hello", file="local.txt")
  on.exit(file.remove("local.txt"), add=TRUE, where="first")
}

Without where="first" (i.e. using where="last" as on.exit() does now),
it all becomes unnecessarily complicated.

Comments?

(*) It seems to come down to adjusting a few lines of code to
do_onexit() [http://svn.r-project.org/R/trunk/src/main/builtin.c] to
control whether the expression should be prepended or appended to the
existing set of recorded expressions.

/Henrik



[Rd] WISHLIST: on.exit(..., add=TRUE, where="first") to address common use cases
Henrik Bengtsson