-
Notifications
You must be signed in to change notification settings - Fork 42
Silently ignore missing arguments in constructor #446
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
Draft
t-kalinowski
wants to merge
6
commits into
main
Choose a base branch
from
constructor-skip-missing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2985a9d
add `collect_dots_skip_missing_`
t-kalinowski 57dabec
update tests
t-kalinowski 16cb422
Convert to snapshot tests.
t-kalinowski 8bdad50
Add tests for `list2()`
t-kalinowski 11aaf2d
Merge branch 'main' into constructor-skip-missing
t-kalinowski 438ae21
Updates after #445
t-kalinowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #define R_NO_REMAP | ||
| #include <R.h> | ||
| #include <Rinternals.h> | ||
|
|
||
| SEXP collect_dots_skip_missing_(SEXP env, SEXP list_dddExprs_call) { | ||
| // This function is equivalent to `base::list(...)`, except it | ||
| // silently skips missing arguments, and auto-names elements | ||
| // that are unnamed and supplied in the call as a symbol. I.e., | ||
| // f(a, , b+1) becomes f(a = a, b+1) | ||
| // | ||
| // Implementation note: ideally we could iterate | ||
| // over the DOTSXP list of promises directly, but there is currently | ||
| // no non-"non-API" way to do this. Approved API promise accessors are | ||
| // pending. So, in the interim, we use `base::missing(..i)` to | ||
| // test for missingness, and use `substitute(list(...))` to get the | ||
| // promise expressions. | ||
| // | ||
| // This same C function can be use to add "skip-missing" and "auto-name" to | ||
| // any function that takes dots. E.g.: | ||
| // | ||
| // list2 <- function(...) .Call(collect_dots_skip_missing_, substitute(list(...))) | ||
| // c2 <- function(...) .Call(collect_dots_skip_missing_, substitute(c(...))) | ||
| // pairlist2 <- function(...) .Call(collect_dots_skip_missing_, substitute(pairlist(...))) | ||
| static SEXP missing_call = NULL; | ||
| if (missing_call == NULL) { | ||
| SEXP missing_fun = Rf_eval(Rf_install("missing"), R_BaseEnv); | ||
| missing_call = Rf_lang2(missing_fun, R_NilValue); | ||
| R_PreserveObject(missing_call); | ||
| } | ||
|
|
||
| static char ddi_buf[14] = ".."; | ||
| static char *i_buf = ddi_buf + 2; | ||
|
|
||
| PROTECT_INDEX pi; | ||
| PROTECT_WITH_INDEX(R_NilValue, &pi); | ||
|
|
||
| { | ||
| unsigned int i = 1; | ||
| SEXP prev_node = list_dddExprs_call; | ||
| SEXP ddExpr_node = CDR(list_dddExprs_call); | ||
| for (; ddExpr_node != R_NilValue; i++) { | ||
| { | ||
| int ret = snprintf(i_buf, sizeof(ddi_buf) - 2, "%u", i); | ||
| if (ret < 0) | ||
| Rf_error("unknown snprintf error"); | ||
| if (ret >= (int)(sizeof(ddi_buf) - 3)) | ||
| Rf_error("snprintf truncated output, too many args in `...`"); | ||
| ddi_buf[sizeof(ddi_buf) - 1] = '\0'; // just in case | ||
| } | ||
|
|
||
| SEXP ddSym = Rf_install(ddi_buf); | ||
|
|
||
| SETCADR(missing_call, ddSym); | ||
| SEXP is_missing = Rf_eval(missing_call, env); | ||
| REPROTECT(is_missing, pi); | ||
|
|
||
| if (Rf_asLogical(is_missing)) { | ||
| // splice out the node from the exprs list. | ||
| ddExpr_node = CDR(ddExpr_node); | ||
| SETCDR(prev_node, ddExpr_node); | ||
| } else { | ||
| // maybe auto-name if unnamed and expr is a symbol. | ||
| if (TAG(ddExpr_node) == R_NilValue) { | ||
| SEXP val_expr = CAR(ddExpr_node); | ||
| if (TYPEOF(val_expr) == SYMSXP) { | ||
| SET_TAG(ddExpr_node, val_expr); | ||
| } | ||
| } | ||
| // replace the node expr with `..i` | ||
| SETCAR(ddExpr_node, ddSym); | ||
| // advance to the next node. | ||
| prev_node = ddExpr_node; | ||
| ddExpr_node = CDR(ddExpr_node); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| UNPROTECT(1); // is_missing | ||
| return Rf_eval(list_dddExprs_call, env); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| test_that("list2() works", { | ||
| # list2() is equivalent to base::list(), with the following differences: | ||
| # - A missing arg value is silently ignored instead of signaling an error. | ||
| # - An argument is automatically named if it is unnamed and the value expression is a symbol. | ||
|
|
||
| expect_identical(list2(), list()) | ||
| expect_identical(list2(a = 1), list(a = 1)) | ||
| expect_identical(list2(a = 1, b = ), list(a = 1)) | ||
| expect_identical(list2(a = 1, b = , , ), list(a = 1)) | ||
| expect_identical(list2(, a = 1, b = , , ), list(a = 1)) | ||
| a <- 1 | ||
| expect_identical(list2(a), list(a = 1)) | ||
| expect_identical(list2(a, b = ), list(a = 1)) | ||
| expect_identical(list2(a, b = , a, ), list(a = 1, a = 1)) | ||
| expect_identical(list2(a = identity(a)), list(a = 1)) | ||
|
|
||
| expect_identical(list2((a)), list(1)) | ||
| expect_identical(list2(identity(a)), list(1)) | ||
|
|
||
| # make sure all this works if values in `...` are nested promises | ||
| f1 <- function(...) list2(...) | ||
| f2 <- function(..., b) f1(..., b) | ||
| f3 <- function(..., c) f2(..., c) | ||
| f4 <- function(..., d) f3(..., d) | ||
|
|
||
| a <- 1; b <- 2 | ||
| for (f in list(f1, f2, f3, f4, list2)) { | ||
| expect_identical(f(), list()) | ||
| expect_mapequal(f(a = 1), list(a = 1)) | ||
| expect_mapequal(f(a = 1, b =), list(a = 1)) | ||
| expect_mapequal(f(a = 1, b = 2), list(a = 1, b = 2)) | ||
| expect_mapequal(f(a, b), list(a = 1, b = 2)) | ||
| } | ||
|
|
||
| expect_identical(list2(a, b, a + b), list(a = 1, b = 2, 3)) | ||
| expect_identical(list2(a, b, c = a + b), list(a = 1, b = 2, c = 3)) | ||
| expect_identical(list2((a), b, c = a + b), list( 1, b = 2, c = 3)) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.