Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructed function environment not quite right #134

Open
hadley opened this issue Nov 16, 2021 · 2 comments
Open

Constructed function environment not quite right #134

hadley opened this issue Nov 16, 2021 · 2 comments

Comments

@hadley
Copy link
Member

hadley commented Nov 16, 2021

Ideally the memoised function would be insulated/namespaced so it's not affected by the global environment

library(memoise)

f <- memoise(function(x) 10)
f()
#> [1] 10

setdiff <- function(...) stop("!")
f()
#> Error in setdiff(names(default_args), names(called_args)): !

Created on 2021-11-16 by the reprex package (v2.0.1)

@wch
Copy link
Member

wch commented Nov 16, 2021

What would the memoized function have for a parent?

Currently, it creates an environment which is a child of the calling environment:

library(memoise)
f <- function(x) 10
fm <- memoise(f)
environment(fm)
#> <environment: 0x10fab1388>
pryr::parenvs(fm)
#>   label                      name
#> 1 <environment: 0x10fab1388> ""  
#> 2 <environment: R_GlobalEnv> ""

A naive alternative would be to create an environment which is a child of the memoise namespace, but then the memoized function would have a dependency on the memoize package, which is something I think we want to avoid.

Maybe it would make sense to make the memoized function environment a sibling of the global env? Or, we may even be able to make it a child of the base environment (or base namespace).

@hadley
Copy link
Member Author

hadley commented Nov 16, 2021

Yeah, maybe make child of base environment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants