-
Notifications
You must be signed in to change notification settings - Fork 58
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
Create compatibility with R6 methods? #133
Comments
I think the way to make this work is to assign it in the constructor: ther6 <- R6::R6Class(
"ther6",
public = list(
initialize = function() {
self$fun <- memoise::memoise(function() {
message("Running fun()")
"foo"
})
},
fun = NULL
)
)
test <- ther6$new()
test$fun()
#> Running fun()
#> [1] "foo"
test$fun()
#> [1] "foo" Note that fun_shared <- memoise::memoise(function() {
message("Running fun()")
"foo"
})
ther6 <- R6::R6Class(
"ther6",
public = list(
initialize = function() {
self$fun <- fun_shared
},
fun = NULL
)
)
test <- ther6$new()
test2 <- ther6$new()
test$fun()
#> Running fun()
#> [1] "foo"
test2$fun()
#> [1] "foo" This obviously won't work well if you want to make use of Another route you could take is to do |
I'm definitely using the
Thanks for the input! |
@wch Could you please give a concrete implementation for
Thank you! |
@teucer here you go: cm <- cachem::cache_mem()
ther6 <- R6::R6Class(
"ther6",
public = list(
initialize = function() {
self$fun <- memoise::memoise(function() {
message("Running fun()")
"foo"
}, cache = cm)
},
fun = NULL
)
)
test <- ther6$new()
test$fun()
#> Running fun()
#> [1] "foo"
test$fun()
#> [1] "foo" |
@wch this seems to be similar to first version. How is it different? I think it would advantageous to be able to memoise only once for the whole class (like python class methods). I tried to initialize |
@teucer Sorry, I copied and pasted the wrong code. I've fixed it in my comment above. |
@wch I'm curious if this approach is frowned-upon (when compared to the 'assign-to-NULL' approach shown above): Klass <- R6::R6Class(
public = list(
echo = function(x) {
print("echoing")
x
},
initialize = function() {
base::unlockBinding("echo", self)
self$echo <- memoise::memoise(self$echo)
base::lockBinding("echo", self)
}
)
)
obj <- Klass$new()
obj$echo(1)
# echoing
# [1]
obj$echo(1)
# [1]
The difference here is Does this have any practical difference in the final objects? I've done some light testing that suggests, "no", but I might be missing a nuanced detail re: redefining methods vs converting a field to a method. (For example, any concerns with |
@mmuurr That looks like OK to me. One thing to keep in mind is that, if the function actually uses If you clone the object, the cloned object's Klass <- R6::R6Class(
public = list(
x = 1,
getx = function() {
print("echoing")
self$x
},
initialize = function() {
base::unlockBinding("getx", self)
self$getx <- memoise::memoise(self$getx)
base::lockBinding("getx", self)
}
)
)
obj <- Klass$new()
obj$getx()
#> [1] "echoing"
#> [1] 1
obj2 <- obj$clone()
obj2$x <- 5
obj2$getx()
#> [1] 1 There may also be problems with inheritance, but I don't know for sure offhand. |
It looks like
memoise
is not compatible with R6 methods. What's the feasibility of creating compatibility with R6?The text was updated successfully, but these errors were encountered: