Skip to content
Prev 7508 / 12125 Next

[R-pkg-devel] R feature suggestion: Duplicated function arguments check

Duncan,

This may not be the place to discuss this so I will be brief.

The question is whether it should be some kind of error to call a function
with two named arguments that are the same.

I can think of a perhaps valid use when a function expects to take the first
few arguments for personal use and then uses ... to pass the rest along to
other functions it calls.

so in your case, slightly extended:

	f(a=1, b=2, a=3, c=-5)

The function might pass along to another function:
	other(arg, ...)
which would be seen as:
	other(arg, a=3, c=-5)

There can of course be other ways to get this result but probably not as
simple. And note this can go several layers deep as various functions call
each other and each has a different need and even meaning for a=something.

Avi
-----Original Message-----
From: R-package-devel <r-package-devel-bounces at r-project.org> On Behalf Of
Duncan Murdoch
Sent: Monday, November 8, 2021 11:04 AM
To: Vincent van Hees <vincentvanhees at gmail.com>;
r-package-devel at r-project.org
Subject: Re: [R-pkg-devel] R feature suggestion: Duplicated function
arguments check
On 08/11/2021 10:29 a.m., Vincent van Hees wrote:
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

______________________________________________
R-package-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel