-
Notifications
You must be signed in to change notification settings - Fork 42
Feature: permit (most) reserved attributes names as property names #582
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ | |
| #' function promise in the default constructor, evaluated at the time the | ||
| #' object is constructed. | ||
| #' @param name Property name, primarily used for error messages. Generally | ||
| #' don't need to set this here, as it's more convenient to supply as | ||
| #' don't need to set this here, as it's more convenient to supply as | ||
| #' the element name when defining a list of properties. If both `name` | ||
| #' and a list-name are supplied, the list-name will be used. | ||
| #' @returns An S7 property, i.e. a list with class `S7_property`. | ||
|
|
@@ -72,12 +72,14 @@ | |
| #' # argument to the default constructor | ||
| #' try(Clock(now = 10)) | ||
| #' args(Clock) | ||
| new_property <- function(class = class_any, | ||
| getter = NULL, | ||
| setter = NULL, | ||
| validator = NULL, | ||
| default = NULL, | ||
| name = NULL) { | ||
| new_property <- function( | ||
| class = class_any, | ||
| getter = NULL, | ||
| setter = NULL, | ||
| validator = NULL, | ||
| default = NULL, | ||
| name = NULL | ||
| ) { | ||
| class <- as_class(class) | ||
| check_prop_default(default, class) | ||
|
|
||
|
|
@@ -119,7 +121,7 @@ check_prop_default <- function(default, class, error_call = sys.call(-1)) { | |
| # The meaning of a `...` prop default needs discussion | ||
| stop(simpleError("`default` cannot be `...`", error_call)) | ||
| } | ||
| if (identical(default, quote(expr =))) { | ||
| if (identical(default, quote(expr = ))) { | ||
| # The meaning of a missing prop default needs discussion | ||
| stop(simpleError("`default` cannot be missing", error_call)) | ||
| } | ||
|
|
@@ -128,11 +130,15 @@ check_prop_default <- function(default, class, error_call = sys.call(-1)) { | |
| return() | ||
| } | ||
|
|
||
| if (class_inherits(default, class)) | ||
| if (class_inherits(default, class)) { | ||
| return() | ||
| } | ||
|
|
||
| msg <- sprintf("`default` must be an instance of %s, not a %s", | ||
| class_desc(class), obj_desc(default)) | ||
| msg <- sprintf( | ||
| "`default` must be an instance of %s, not a %s", | ||
| class_desc(class), | ||
| obj_desc(default) | ||
| ) | ||
|
|
||
| stop(simpleError(msg, error_call)) | ||
| } | ||
|
|
@@ -188,7 +194,8 @@ prop_default <- function(prop, envir, package) { | |
| #' lexington@height <- 14 | ||
| #' prop(lexington, "height") <- 15 | ||
| prop <- function(object, name) { | ||
| .Call(prop_, object, name) | ||
| attr_name <- remap_reserved_names(name) | ||
| .Call(prop_, object, name, attr_name) | ||
| } | ||
|
Comment on lines
196
to
199
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the primary change. It only differs from Attributes get remapped at this point so that they keep their user-facing name for all other purposes. |
||
|
|
||
| propr <- function(object, name) { | ||
|
|
@@ -229,11 +236,12 @@ prop_obj <- function(object, name) { | |
| #' [validate()] on the object before returning. | ||
| #' @export | ||
| `prop<-` <- function(object, name, check = TRUE, value) { | ||
| .Call(prop_set_, object, name, check, value) | ||
| attr_name <- remap_reserved_names(name) | ||
| .Call(prop_set_, object, name, attr_name, check, value) | ||
| } | ||
|
|
||
| `propr<-` <- local({ | ||
| # reference implementation of `prop<-()` implemented in R | ||
| # reference implementation of `prop<-()` implemented in R | ||
| # This flag is used to avoid infinite loops if you are assigning a property from a setter function | ||
| setter_property <- NULL | ||
|
|
||
|
|
@@ -246,7 +254,11 @@ prop_obj <- function(object, name) { | |
| } | ||
|
|
||
| if (!is.null(prop$getter) && is.null(prop$setter)) { | ||
| msg <- sprintf("Can't set read-only property %s@%s", obj_desc(object), name) | ||
| msg <- sprintf( | ||
| "Can't set read-only property %s@%s", | ||
| obj_desc(object), | ||
| name | ||
| ) | ||
| stop(msg, call. = FALSE) | ||
| } | ||
|
|
||
|
|
@@ -293,7 +305,8 @@ prop_error_unknown <- function(object, prop_name) { | |
| # called from src/prop.c | ||
| prop_validate <- function(prop, value, object = NULL) { | ||
| if (!class_inherits(value, prop$class)) { | ||
| return(sprintf("%s must be %s, not %s", | ||
| return(sprintf( | ||
| "%s must be %s, not %s", | ||
| prop_label(object, prop$name), | ||
| class_desc(prop$class), | ||
| obj_desc(value) | ||
|
|
@@ -319,7 +332,8 @@ prop_validate <- function(prop, value, object = NULL) { | |
|
|
||
| stop(sprintf( | ||
| "%s validator must return NULL or a character, not <%s>.", | ||
| prop_label(object, prop$name), typeof(val) | ||
| prop_label(object, prop$name), | ||
| typeof(val) | ||
| )) | ||
| } | ||
|
|
||
|
|
@@ -363,7 +377,15 @@ prop_names <- function(object) { | |
|
|
||
| if (inherits(object, "S7_class")) { | ||
| # S7_class isn't a S7_class (somewhat obviously) so we fake the property names | ||
| c("name", "parent", "package", "properties", "abstract", "constructor", "validator") | ||
| c( | ||
| "name", | ||
| "parent", | ||
| "package", | ||
| "properties", | ||
| "abstract", | ||
| "constructor", | ||
| "validator" | ||
| ) | ||
| } else { | ||
| class <- S7_class(object) | ||
| props <- attr(class, "properties", exact = TRUE) | ||
|
|
@@ -473,7 +495,6 @@ as_properties <- function(x) { | |
| } | ||
|
|
||
| as_property <- function(x, name, i) { | ||
|
|
||
| if (is_property(x)) { | ||
| if (name == "") { | ||
| if (is.null(x$name)) { | ||
|
|
@@ -502,4 +523,3 @@ prop_is_read_only <- function(prop) { | |
| prop_has_setter <- function(prop) is.function(prop$setter) | ||
|
|
||
| prop_is_dynamic <- function(prop) is.function(prop$getter) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When arguments are passed through
new_object()they are assigned to attribute values directly, so they go through the same remapping for reserved names here.