-
Notifications
You must be signed in to change notification settings - Fork 57
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
Cannot add functions to R6 that was declared without any public functions #51
Comments
Same behaviour for private functions - you have to have at least one defined in order to add any later |
I noticed the same thing about a month ago, see here. If you take a look at the source code, in r6_class.R, the public and private fields and methods are initialized this way : generator$public_fields <- get_nonfunctions(public)
generator$private_fields <- get_nonfunctions(private)
generator$public_methods <- get_functions(public)
generator$private_methods <- get_functions(private) where get_functions <- function(x) {
funcs <- vapply(x, is.function, logical(1))
if (all(!funcs)) return(NULL)
x[funcs]
} So, if there are no functions in generator_funs$set <- function(which = NULL, name = NULL, value, overwrite = FALSE) {
(...)
# Find which group this object should go in.
if (which == "public") {
group <- if (is.function(value)) "public_methods" else "public_fields"
} else if (which == "private") {
group <- if (is.function(value)) "private_methods" else "private_fields"
} else if (which == "active") {
if (is.function(value))
group <- "active"
else
stop("Can't add non-function to active")
}
(...)
# Assign in correct group
self[[group]][[name]] <- value
(...)
} In the last line, you'll see that it tries to add the new |
If I declare an R6 object and try to add a public function later, I get an error
I seem to have to have at least one public function in the object definition to be able to do this without error
The text was updated successfully, but these errors were encountered: