octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Default arguments


From: John W. Eaton
Subject: Re: Default arguments
Date: Fri, 15 Dec 2006 11:07:16 -0500

On 15-Dec-2006, Tom Holroyd (NIH/NIMH) [E] wrote:

| 
| > It would also be better to be able to call foo like
| > 
| >     foo(arg = 42)
| 
| That could break existing code, though, because that's valid syntax now (set 
the local variable arg).

Yes, it would definitely cause trouble for Octave, though we could
consider a new operator, like

  foo (arg := 42)

But in Matlab assignment is not an expression, so the syntax you
quoted does not presently work.  So if they want to, I think they
could make it have the keyword argument meaning as a special case in
this context.

One problem I have with keyword arguments is that if they are allowed
for any function argument, then you have to carefully choose the names
of all function parameters, and once you've selected them, you can't
change them without the potential for breaking a lot of code.  There
would also be problems with the way variable arguments are overloaded
in some Matlab functions.  For example, being able to call a function
like this:

  axis (limits);

or

  axis (h, limits);

(sorry if some, or at least one, of you think this is "crap", but this
is the way many functions in Matlab already work, so we don't get a
choice here).

When you define this function, you have to write something like

  function axis (h, limits)
    if (nargin == 1)
      handle = gca ();
      limits = h;
    else if (nargin == 2)
      handle = h
    else
      print_usage ();
    endif
    ...

So given the way that many functions in Matlab are defined, I don't
think there is much possibility to handle keyword arguments in any
reasonable way based simply on the names that are used in the function
parameter list.  It just seems to me like it would be a mess.

In any case, Matlab already seems to be wedded to keyword-value pairs
written this way:

  set (h, 'key1', val1, 'key2', val2

so I doubt that Matlab will change to some other way of doing this.
OTOH, I could be wrong.

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]