x <- function(x1, x2){
theCall <- lapply(as.list(match.call()),as.list)[-1]
args <- lapply(theCall, function(x) as.list(formals(as.character(x))))
Map(function(a, b) {
b <- b[-1]
for (i in seq_along(a)) {
if(i <= length(b)) a[i] <- b[i]
}
a
}, args, theCall)
}
str(x(a(3,4), b(5)))
#List of 2
# $ x1:List of 2
# ..$ x: num 3
# ..$ y: num 4
# $ x2:List of 2
# ..$ b1: num 5
# ..$ b2: num 7
显然,即使使用有效的函数调用,也很容易破坏这一点:
str(x(a(3,4), b(,b1 = 5)))
让此函数对所有可能的输入都正确,这是读者的练习。