-
Notifications
You must be signed in to change notification settings - Fork 279
Infix attr accessor #69
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
Comments
I've always been frustrated that I suppose it would be highly inappropriate to force the matter and export a redefinition? `@` <- function(x, slot) {
slot <- as.character(substitute(slot))
if (isS4(x)) {
env <- new.env(parent = parent.frame())
env$`@` <- base::`@`
eval(call("@", x, slot), envir = env)
} else {
attr(x, slot)
}
}
mtcars@names |
Yeah, we can't override existing functions, so I think |
would this follow then? x %@% "foo" <- "bar" # attr(x,"foo") <- "bar" // x.attr("foo") = "bar"; OR use if so then what of attributes(x) <- list(mycomment = "really special", foo = 3:2,bar = paste(1:6)) x %<@% list(mycomment = "really special", foo = 3:2,bar = paste(1:6)) then you need an |
The first one works: `%@%` <- attr
`%@%<-` <- `attr<-`
mtcars %@% "names"
mtcars %@% "names" <- 1:11 The idea of overloading these operators to work with lists seems interesting. `%@%` <- function(x, slots) {
if (length(slots) > 1) {
attributes(x)[slots]
} else {
attr(x, slots)
}
}
`%@%<-` <- function(x, slots, value) {
if (length(slots) > 1) {
attributes(x)[slots] <- value
x
} else {
attr(x, slots) <- value
}
}
mat <- as.matrix(mtcars)
mat %@% c("dimnames", "dim")
mat %@% c("dimnames", "dim") <- list(NULL, c(2, 16, 11)) But it's not good to have the output type change as a function of the number of slots requested. So we'd have to choose one of those two behaviours. The most useful is the list of attributes accessor probably, and we can still hope that R core will enable single attribute lookup with |
One possibility is to make the behaviour depend on whether the slot is a vector or a list. But the syntax becomes kind of heavy then mat %@% list("dimnames", "dim") <- list(NULL, c(2, 16, 11)) |
I'd rather keep it simple and stick with just getting of single values. |
Closed in 97ddec7 |
Maybe purrr would be a good place for:
Need to think about if this should be S3 generic or not, and need to check if this works as is for S4.
The text was updated successfully, but these errors were encountered: