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

Implement $ and $<- methods #204

Merged
merged 5 commits into from
Mar 9, 2022
Merged

Implement $ and $<- methods #204

merged 5 commits into from
Mar 9, 2022

Conversation

hadley
Copy link
Member

@hadley hadley commented Mar 8, 2022

Fixes #198

@hadley hadley requested a review from DavisVaughan March 8, 2022 14:45
R/zzz.R Outdated Show resolved Hide resolved
R/zzz.R Outdated Show resolved Hide resolved
R/zzz.R Show resolved Hide resolved
@@ -16,6 +16,35 @@ R7_object <- new_class(
)
methods::setOldClass("R7_object")

#' @export
`$.R7_object` <- function(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.

Somewhat interesting to note that if:

# rather than this, where the integer vector is the core data object
new_class("foo", parent = "integer")

# we instead used this as the implementation
# (where new_class("foo", parent = "integer") would basically do this instead)
new_class("foo", properties = list(data = "integer"))

Then we could drop @ altogether and use $ to access the properties, one of which would be $data.

I know you mentioned that we probably want a way to directly build on base atomic objects, so it probably is too late, but it is interesting to think about how @ could be removed (that would probably make autocomplete work on properties out of the box too, probably through .DollarNames)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess you still have to think about whether or not you want this with lists though

# if this:
new_class("foo", parent = "list")

# was basically this:
foo <- new_class("foo", properties = list(data = "list")

obj <- foo(list(x = 1))

# then you'd have to do this:
obj$data$x

I'm not sure I mind this though, since the argument name is foo(.data=). And it isn't like we were able to do obj <- foo(x = 1), so I could be convinced that I should expect to have to go through $data to get to the list elements

That would also remove this ambiguity about when you can use $ on an R7 object

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the problem is then that is.list() would be TRUE for R7 objects which seems likely to lead to a bunch of unintended downstream consequences.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was still thinking the core object would be that weird S4 gremlin thing that is returned by R7_object(), and we'd still have all the properties as attributes on that

}
#' @export
`$<-.R7_object` <- function(x, name, value) {
if (typeof(x) %in% c("list", "environment")) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question about expressions

@hadley hadley merged commit b3f18cd into main Mar 9, 2022
@hadley hadley deleted the dollar-help branch March 9, 2022 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide $.R7_method
2 participants