Thanks for your replies, I only just noticed them as I had daily-digest = true and someone seems to have removed my e-mail address when replying to the list. Never mind, I have now switched my daily-digest to false.
Yes - The issue is that "myfun(A = 1, B = 2, B = 4)" in the example below is ambiguous and only the end-user knows whether B should be 2 or 4. Either way, I will just implement my own solution. Just wanted to report that I ran into this situation and maybe others too. Best, Vincent On Mon, 8 Nov 2021 at 17:24, Vincent van Hees <vincentvanh...@gmail.com> wrote: > Thanks Duncan, I have tried to make a minimalistic example: > > myfun = function(...) { > input = list(...) > mysum = function(A = c(), B= c()) { > return(A+B) > } > if ("A" %in% names(input) & "B" %in% names(input)) { > print(mysum(A = input$A, B = input$B)) > } > } > > # test: > > myfun(A = 1, B = 2, B = 4) > [1] 3 > > # So, the second B is ignored. > > > > On Mon, 8 Nov 2021 at 17:03, Duncan Murdoch <murdoch.dun...@gmail.com> > wrote: > >> On 08/11/2021 10:29 a.m., Vincent van Hees wrote: >> > Not sure if this is the best place to post this message, as it is more >> of a >> > suggestion than a question. >> > >> > When an R function accepts more than a handful of arguments there is the >> > risk that users accidentally provide arguments twice, e.g myfun(A=1, >> B=2, >> > C=4, D=5, A=7), and if those two values are not the same it can have >> > frustrating side-effects. To catch this I am planning to add a check for >> > duplicated arguments, as shown below, in one of my own functions. I am >> now >> > wondering whether this would be a useful feature for R itself to >> operate in >> > the background when running any R function that has more than a certain >> > number of input arguments. >> > >> > Cheers, Vincent >> > >> > myfun = function(...) { >> > #check input arguments for duplicate assignments >> > input = list(...) >> > if (length(input) > 0) { >> > argNames = names(input) >> > dupArgNames = duplicated(argNames) >> > if (any(dupArgNames)) { >> > for (dupi in unique(argNames[dupArgNames])) { >> > dupArgValues = input[which(argNames %in% dupi)] >> > if (all(dupArgValues == dupArgValues[[1]])) { # double >> arguments, >> > but no confusion about what value should be >> > warning(paste0("\nArgument ", dupi, " has been provided more >> than >> > once in the same call, which is ambiguous. Please fix.")) >> > } else { # double arguments, and confusion about what value >> should >> > be, >> > stop(paste0("\nArgument ", dupi, " has been provided more >> than >> > once in the same call, which is ambiguous. Please fix.")) >> > } >> > } >> > } >> > } >> > # rest of code... >> > } >> > >> >> Could you give an example where this is needed? If a named argument is >> duplicated, R will catch that and give an error message: >> >> > f(a=1, b=2, a=3) >> Error in f(a = 1, b = 2, a = 3) : >> formal argument "a" matched by multiple actual arguments >> >> So this can only happen when it is an argument in the ... list that is >> duplicated. But usually those are passed to some other function, so >> something like >> >> g <- function(...) f(...) >> >> would also catch the duplication in g(a=1, b=2, a=3): >> >> > g(a=1, b=2, a=3) >> Error in f(...) : >> formal argument "a" matched by multiple actual arguments >> >> The only case where I can see this getting by is where you are never >> using those arguments to match any formal argument, e.g. >> >> list(a=1, b=2, a=3) >> >> Maybe this should have been made illegal when R was created, but I think >> it's too late to outlaw now: I'm sure there are lots of people making >> use of this. >> >> Or am I missing something? >> >> Duncan Murdoch >> > [[alternative HTML version deleted]] ______________________________________________ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel