Creating a promise using delayedAssign(), it is easy to construct a scenario which might lead to the emission of a restarting interrupted promise evaluation warning. Why is a warning thrown in such a scenario? Does this warning indicate "unsafe" practices or is this more of an "fyi" and can safely be ignored (i.e. muffled)? Consider the following example:
counter <- 0L
make_val <- function() {
if (counter < 1L) {
counter <<- counter + 1L
stop("insufficient count")
}
"yay!"
}
delayedAssign("foo", make_val())
foo
#> Error in make_val() : insufficient count
foo
#> [1] "yay!"
#> Warning message:
#> restarting interrupted promise evaluation
foo
#> [1] "yay!"
Is it safe to silence this warning? Or is restarting an interrupted promise evaluation something that should be avoided?
get_foo <- function() {
mute_ripe <- function(warn) {
if (identical(conditionMessage(warn),
"restarting interrupted promise evaluation")) {
invokeRestart("muffleWarning")
}
}
tryCatch(
withCallingHandlers(get("foo", envir = .GlobalEnv), warning = mute_ripe),
error = function(err) NULL
)
}
counter <- 1L
delayedAssign("foo", make_val())
get_foo()
#> NULL
get_foo()
#> [1] "yay!"
get_foo()
#> [1] "yay!"