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

Take list name from property name #379

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# S7 (development version)

* In `new_class()`, properties can either be named by naming the element
of the list or by supplying the `name` argument to `new_property()` (#371).

* S7 provides a new automatic backward compatibility mechanism to provide
a version of `@` that works in R before version 4.3 (#326).

Expand Down
19 changes: 13 additions & 6 deletions R/property.R
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ as_properties <- function(x) {
}

out <- Map(as_property, x, names2(x), seq_along(x))
names(out) <- names2(x)
names(out) <- vapply(out, function(x) x$name, FUN.VALUE = character(1))

if (anyDuplicated(names(out))) {
stop("`properties` names must be unique", call. = FALSE)
Expand All @@ -403,15 +403,22 @@ as_properties <- function(x) {
}

as_property <- function(x, name, i) {
if (name == "") {
msg <- sprintf("`property[[%i]]` is missing a name", i)
stop(msg, call. = FALSE)
}

if (is_property(x)) {
x$name <- name
if (is.null(x$name)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So an existing property $name has priority over the name supplied in the list() call if you supply both, right? I can't tell if that seems right or not, but don't have strong feelings either way

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I think the list name should win.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was also my gut feeling

if (name == "") {
msg <- sprintf("`property[[%i]]` must have a name or be named.", i)
hadley marked this conversation as resolved.
Show resolved Hide resolved
stop(msg, call. = FALSE)
}
x$name <- name
}
x
} else {
if (name == "") {
msg <- sprintf("`property[[%i]]` must be named.", i)
hadley marked this conversation as resolved.
Show resolved Hide resolved
stop(msg, call. = FALSE)
}

class <- as_class(x, arg = paste0("property$", name))
new_property(x, name = name)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/property.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@
as_properties(list(1))
Condition
Error:
! `property[[1]]` is missing a name
! `property[[1]]` must be named.
Code
as_properties(list(new_property(class_character)))
Condition
Error:
! `property[[1]]` is missing a name
! `property[[1]]` must have a name or be named.
Code
as_properties(list(x = 1))
Condition
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-property.R
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ test_that("as_properties normalises properties", {
as_properties(list(x = new_property(class = class_numeric))),
list(x = new_property(class_numeric, name = "x")
))
expect_equal(
as_properties(list(new_property(name = "y"))),
list(y = new_property(name = "y")
))
})

test_that("as_properties() gives useful error messages", {
Expand Down
Loading