Skip to content

On the mechanics of function evaluation and argument matching

6 messages · Brian Lee Yung Rowe, R. Michael Weylandt, Ben Bolker +1 more

#
On Wed, Jul 17, 2013 at 9:58 AM, Brian Rowe <rowe at muxspace.com> wrote:
I'm not sure I can, but I'd look around at how the missing() function is used.

Cheers,
MW
#
Thanks for the lead. Given the example in ?missing though, wouldn't it be safer to explicitly define a default value of NULL:

myplot <- function(x, y=NULL) {
  if(is.null(y)) {
    y <- x
    x <- 1:length(y)
  }
  plot(x, y)
}
On Jul 17, 2013, at 11:05 AM, "R. Michael Weylandt" <michael.weylandt at gmail.com> wrote:

            
#
Brian Rowe <rowe <at> muxspace.com> writes:
[snip]

 In my opinion the missing() functionality can indeed be
fragile (for example, I don't know how I can manipulate an
existing call to make an argument be 'missing' when it was
previously 'non-empty') and using an explicit NULL is often
a good idea.  This makes the documentation a tiny bit less
wieldy if you have lots of parameters ...
#
I agree that failing fast is a good principle. My initial point led the other way though, i.e. any unmatched formal arguments without default values should be handled in one of two ways:

1. Fail the function call. This is what most non-functional languages do e.g. Python
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes exactly 3 arguments (1 given)

2. Perform partial application, like some functional languages e.g. Haskell
f :: Int -> Int -> Int -> Int
f x y z = x

*Main> let a = f 2
*Main> :t a
a :: Int -> Int -> Int

Otherwise if an argument is truly optional, I don't see why a default value cannot be assigned to the formal argument when defining the function (excepting the edge cases you pointed out earlier).

Brian
On Jul 17, 2013, at 2:35 PM, Peter Meilstrup <peter.meilstrup at gmail.com> wrote: