Skip to content

Commit

Permalink
Apply omit_args also to default arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich committed Mar 6, 2023
1 parent 3b0bf3e commit 3cf3234
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# memoise (development version)

* `omit_args` now also works for default arguments (@mgirlich, #145).

# memoise 2.0.1

# Version 2.0.0.9000
Expand Down
6 changes: 3 additions & 3 deletions R/memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ memoise <- memoize <- function(
# That has not been called
default_args <- default_args[setdiff(names(default_args), names(called_args))]

# Ignored specified arguments when hashing
called_args[encl$`_omit_args`] <- NULL

# Evaluate all the arguments
args <- c(lapply(called_args, eval, parent.frame()),
lapply(default_args, eval, envir = environment()))

# Ignored specified arguments when hashing
args[encl$`_omit_args`] <- NULL

key <- encl$`_hash`(
c(
encl$`_f_hash`,
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-memoise.R
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ test_that("omit_args respected", {
res2 <- mem_rnorm(10, mean = +100)

expect_true(identical(res1, res2))

# Also works for default args
f <- function(n, x = rnorm(1)) {
rnorm(n)
}
mem_f <- memoise(f)
expect_false(identical(res1, res2))
res1 <- mem_f(1)
res2 <- mem_f(1)

mem_f2 <- memoise(f, omit_args = "x")
res1 <- mem_f2(1)
res2 <- mem_f2(1)
expect_true(identical(res1, res2))
})

context("has_cache")
Expand Down

0 comments on commit 3cf3234

Please sign in to comment.