r-lib/rlang

Minor `?env_unbind` issues: `inherit` example, undocumented behaviors

Opened this issue · 0 comments

?env_unbind seems to imply that inherit = TRUE will remove a binding from all ancestors:

# With inherit = TRUE, it removes bindings in parent environments
# as well:
parent <- env(empty_env(), foo = 1, bar = 2)
env <- env(parent, foo = "b")

env_unbind(env, "foo", inherit = TRUE)
env_has(env, c("foo", "bar"))
env_has(env, c("foo", "bar"), inherit = TRUE)

but this is not the case, with the last two lines evaluating to

env_has(env, c("foo", "bar"))
#>   foo   bar 
#> FALSE FALSE 
env_has(env, c("foo", "bar"), inherit = TRUE)
#>  foo  bar 
#> TRUE TRUE 

Perhaps this could become

# With inherit = TRUE, it can remove bindings in parent environments
# as well:
parent <- env(empty_env(), foo = 1, bar = 2)
env <- env(parent, foo = "b")

env_unbind(env, "foo", inherit = TRUE) # removes from `env`
env_unbind(env, "foo", inherit = TRUE) # removes from `parent`
env_has(env, c("foo", "bar"))
#>   foo   bar 
#> FALSE FALSE 
env_has(env, c("foo", "bar"), inherit = TRUE)
#>   foo   bar 
#> FALSE  TRUE 

Additionally, it would be nice if the documentation guaranteed [a certain warning/error behavior when attempting to remove a binding that doesn't exist. Currently, that behavior is no error or warning if no binding is found to remove. (I originally thought this matched mutate behavior and would be handy for implementing mutate-like verbs, but although mutate allows you to "remove" nonexistent columns, the error message when attempting to remove grouping columns (! `vars` missing from `data`: [...]) suggests that's not supposed to be the case. So name-checking logic still seems like something that would need to be handled downstream in this case.)]