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

Better err msg for read-only active bindings #100

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/clone.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ generator_funs$clone_method <- function(deep = FALSE) {

if (length(active_copies) > 0) {
for (name in names(active_copies)) {
makeActiveBinding(name, active_copies[[name]], public_bind_env)
make_active_binding(name, active_copies[[name]], public_bind_env)
}
}

Expand Down
4 changes: 2 additions & 2 deletions R/new.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ generator_funs$new <- function(...) {
# Set up active bindings ------------------------------------------
if (!is.null(active)) {
for (name in names(active)) {
makeActiveBinding(name, active[[name]], public_bind_env)
make_active_binding(name, active[[name]], public_bind_env)
}
}

Expand Down Expand Up @@ -223,7 +223,7 @@ encapsulate({
list2env2(public_methods, envir = super_bind_env)
list2env2(private_methods, envir = super_bind_env)
for (name in names(active)) {
makeActiveBinding(name, active[[name]], super_bind_env)
make_active_binding(name, active[[name]], super_bind_env)
}

# Return an object with all the information needed to merge down
Expand Down
23 changes: 23 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ encapsulate({
x[!funcs]
}
})

#' Wraps \code{\link[base]{makeActiveBinding}}, providing a better
#' error message for read-only bindings.
#' @param sym A string naming the variable to bind to.
#' @param fun A function to bind to the variable.
#' @param env An environment to create the binding in.
#' @return An active binding.
#' @examples
#' fn <- function() 1
#' make_active_binding("var", fn, globalenv())
#' var
#' try(var <- 123)
#' @noRd
make_active_binding <- function(sym, fun, env) {
new_fun <- if(length(formals(fun)) == 0L) {
function(value) {
if(!missing(value)) {
stop(sym, " is read-only.")
} else fun()
}
} else fun
makeActiveBinding(sym, new_fun, env)
}