-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Labels
Description
This discussion was raised in #739 , by using ... after arguments that should or are frequently provided you can force users in making argument names explicit. This can help prevent accidental usage of argument and makes the code less sensitive to changes in the order of arguments. Below there are some examples. Would it be worth starting to use this into the future?
aa<-function(x, ..., d, e){
if(!missing(e))
{
cli::cli_inform("e: {e}")
}
if(!missing(d))
{
cli::cli_inform("d: {d}")
}
return(x)
}
ad<-function(x, ..., d, e){
rlang::check_dots_used()
if(!missing(e))
{
cli::cli_inform("e: {e}")
}
if(!missing(d))
{
cli::cli_inform("d: {d}")
}
return(x)
}
ab<-function(x, d, e){
if(!missing(e))
{
cli::cli_inform("e: {e}")
}
if(!missing(d))
{
cli::cli_inform("d: {d}")
}
return(x)
}
# In this case were not forced to make explicit what the 3 is meant fors
ab(2, 3)
#> d: 3
#> [1] 2
# By using dots the second argument has to be made explicit otherwise it is not used
aa(2, 3)
#> [1] 2
# Using rlang we can force an error if not all arguments can be matched
ad(2, 3)
#> Error in `ad()`:
#> ! Arguments in `...` must be used.
#> ✖ Problematic argument:
#> • ..1 = 3
#> ℹ Did you misspell an argument name?
# it all behaves the same when the argument is made explicit
ab(2, e=3)
#> e: 3
#> [1] 2
aa(2, e=3)
#> e: 3
#> [1] 2
ad(2, e=3)
#> e: 3
#> [1] 2PietrH