-
Notifications
You must be signed in to change notification settings - Fork 5
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
Alternative way of passing arguments to methods #48
Comments
Thanks. I don't understand "extra work for NSE". |
Here is a POC: # krlmlr said this needed the ellipsis but I'm not sure why
# this is exported, documented, and referenced in `?construct`
opts_function <- function(trim = NULL, as.function = FALSE, zap_srcref = FALSE, construct_env = FALSE) {
# insert check and processing of args here
structure(
class = c("constructive_options", "constructive_options_function"),
list(
trim = trim,
as.function = as.function,
zap_srcref = zap_srcref,
construct_env = construct_env
)
)
}
fetch_opts <- function(suffix, ...) {
class <- paste0("constructive_options_", suffix)
Filter(function(x) inherits(x, class) ,list(...))[[1]]
}
construct_idiomatic.function <- function(x, ...) {
args <- fetch_opts("function", ...)
args
# actual logic, we can pass the dots without further processing
}
construct <- function(x, ...) {
if (is.function(x)) construct_idiomatic.function(x, ...)
}
construct(ave, opts_function(zap_srcref = TRUE))
#> $trim
#> NULL
#>
#> $as.function
#> [1] FALSE
#>
#> $zap_srcref
#> [1] TRUE
#>
#> $construct_env
#> [1] FALSE
#>
#> attr(,"class")
#> [1] "constructive_options" "constructive_options_function" How to use a template ?
How to modify options and propagate a new value ? We can copy |
@krlmlr It doesn't matter for this package but I was thinking that in general to use the approach used above in my POC, having NSE arguments to |
The ellipsis in the front enforces the use of named arguments, and provides an opportunity for better error messages and for renaming options. We could also do a separate help page for each option constructor. |
Right I forgot this detail thanks
Yes I had this in mind, I meant exported, documented on its own, and linked in |
|
Regarding the template, I now believe it should be a separate argument This simplify the handling of the This way the user can personalise once for all the way they want construct() to work, this function should be quick and easy to call and it should be easy to save preferences.
We should be able to integrate general options and waldo options to the template too, to maybe these should be wrapped into arguments passed to |
@krlmlr made an interesting suggestion
Current implementation and ideas in #34 pose a few challenges :
The suggestion, is to use additional functions that return "option" objects. These objects are fed unnamed to the
...
and are recognised by the relevant method (we need a helper for this too).This would additionally provide a way to isolate the argument checking/processing from the logic.
The downside is :
constructive::
several times)The upside seems bigger though.
The text was updated successfully, but these errors were encountered: