octave-maintainers
[Top][All Lists]
Advanced

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

Re: functions with 'named' arguments


From: John W. Eaton
Subject: Re: functions with 'named' arguments
Date: Wed, 19 Mar 2008 21:12:08 -0400

On 19-Mar-2008, Dupuis wrote:

| For the first remark: a grep on all files under /usr/share/octave/3.0.0/
| showed that the pattern
| '([^<>!=)]*=[^=)]*)' is only found within strings or if clauses. So the
| pattern of default arguments seems not yet used inside Octave sources.

It's used in one file in the current sources, and I would like to se
it used in more frequently to replace the code like

  if (nargin < 4)
    arg4 = default_val;
  endif

that we currently have in many Octave functions.

In any case, I think the use of default function parameter values in
function definitions is completely seprate from the issue of whether
to support named arguments which are used in function calls, not
function definitions.

The only way that we could support named arguments in funtion calls is
with some syntax other than "name = value".  I think we could use
"name := value" but as Shai noted, this would be an extension to
Matlab and those things tend to come back to bite us later.

Also, as others have said, the typical Matlab style is to either use
keyword/value pairs or an option structure and although it is a bit
more work for the person writing the function, I think it does provide
approximately the same functionality as named arguments for the person
calling the function.

Finally, if you really want this feature, then I think you should
submit a patch.  But I think that adding this feature might not be
trivial.  Here are some things to think about that may cause trouble:

  * Is there a way to separate positional arguments from named
    arguments, or are all arguments allowed to be passed as named
    arguments?

  * Do functions need to declare that they accept named arguments, or
    are named arguments automatically allowed for all function calls?

  * How should named arguments be handled when the argument is not
    supplied when the function is called?  For example, given the
    function definition

      foo (a, b)
        ...
      endfunction

    what happens when you call it with

      foo (b = 13)

    ?  What value is assigned to A?  What is the value of nargin?  If
    all functions are allowed to be called with named arguments, and
    some are allowed to be omitted when the function is called, then I
    think this will cause some trouble with the way nargin is usually
    used, since normally if nargin == 1, that means that the first
    argument has been provided.

So maybe adding named arguments is not just a simple matter of syntax.

jwe


reply via email to

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