inst/testme/test-globals,subassignment.R

#' @tags globals
#' @tags detritus-files
#' @tags mirai_multisession

library(future.mirai)

plan(mirai_multisession)

oopts <- c(oopts, options(
  future.globals.resolve = TRUE,
  future.globals.onMissing = "error"
))


message("*** Globals - subassignments ...")

message("*** Globals - subassignments w/ x$a <- value ...")

## Truth:
x <- x0 <- list()
y0 <- list(a = 1)
str(list(x = x, y0 = y0))

y <- local({
  x$a <- 1
  x
})
stopifnot(identical(y, y0))

y <- local({
  x[["a"]] <- 1
  x
})
stopifnot(identical(y, y0))

y <- local({
  x["a"] <- list(1)
  x
})
stopifnot(identical(y, y0))

stopifnot(identical(x, list()))

## Explicit future
x <- list()
f <- future({
  x$a <- 1
  x
})
rm(list = "x")
y <- value(f)
print(y)
stopifnot(identical(y, y0))

## Future assignment
x <- list()
y %<-% {
  x$a <- 1
  x
}
rm(list = "x")
print(y)
stopifnot(identical(y, y0))

## 'x' is _not_ a global variable here
x <- list()
y %<-% {
  x <- list(b = 2)
  x$a <- 1
  x
}
rm(list = "x")
print(y)
stopifnot(identical(y, list(b = 2, a = 1)))

## Explicit future
x <- list()
f <- future({
  x[["a"]] <- 1
  x
})
rm(list = "x")
y <- value(f)
print(y)
stopifnot(identical(y, y0))

## Future assignment
x <- list()
y %<-% {
  x[["a"]] <- 1
  x
}
rm(list = "x")
print(y)
stopifnot(identical(y, y0))

## Explicit future
x <- list()
f <- future({
  x["a"] <- list(1)
  x
})
rm(list = "x")
y <- value(f)
print(y)
stopifnot(identical(y, y0))

## Future assignment
x <- list()
y %<-% {
  x["a"] <- list(1)
  x
}
rm(list = "x")
print(y)
stopifnot(identical(y, y0))

## Future assignment
x <- list()
name <- "a"
y %<-% {
  x[name] <- list(1)
  x
}
rm(list = c("x", "name"))
print(y)
stopifnot(identical(y, y0))

message("*** Globals - subassignments w/ x$a <- value ... DONE")

message("*** Globals - subassignments ... DONE")

plan(sequential)

Try the future.mirai package in your browser

Any scripts or data that you put into this service are public.

future.mirai documentation built on June 8, 2025, 12:43 p.m.