-
Notifications
You must be signed in to change notification settings - Fork 32
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
Conversation
@@ -16,6 +16,35 @@ R7_object <- new_class( | |||
) | |||
methods::setOldClass("R7_object") | |||
|
|||
#' @export | |||
`$.R7_object` <- function(x, name) { |
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.
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
)
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.
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
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.
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.
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.
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")) { |
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.
Same question about expressions
Co-authored-by: Davis Vaughan <davis@rstudio.com>
Conflicts: NEWS.md
Fixes #198