octave-maintainers
[Top][All Lists]
Advanced

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

Re: functions with 'named' arguments


From: Dupuis
Subject: Re: functions with 'named' arguments
Date: Fri, 21 Mar 2008 10:55:38 -0700 (PDT)



John W. Eaton wrote:
> 
> 
> 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.
> 
> 
Hello John,

It seems that R uses this approach:
1) create two lists, one (latter called the first) with the arguments from
the function definition, one (latter called the second list) with the
arguments in the actual call
2) move the named arguments from the second list to a third list, and remove
the arguments with corresponding names from the first. Perform a second step
of matching in a first come, first serve fashion.
3)  match the arguments from the third list to the arguments of the first
list. 
4) check for errors:
   - unmatched arguments in the first list without default value => error
message, stop processing
   - extraneous, unmatched arguments, either named or not, and no varargin
=> error message, stop processing 
    - extraneous arguments are moved to varargin
So:
- an argument without default value is mandatory
- extraneous arguments are either processed by subfunctions, or cause error
message if unrecognised.

This means:
1) arguments are either positionnal or named, without anything special at
the function definition
2)  foo = function(a, b) {
+ print(a);
+ print(b);
+ }
foo(b=3) => error message, a is missing and no default value given
foo(b=3, a=2) => disp a then b
foo(b=3, a=2, b=5) => error: formal argument b corresponding to more than
one input argument 

Finally, about the nargin: there should be something equivalent, but I still
didn't find where. 
Here are some examples of R calls:
http://blog.moertel.com/articles/2006/01/20/wondrous-oddities-rs-function-call-semantics

Regards 

Pascal
-- 
View this message in context: 
http://www.nabble.com/functions-with-%27named%27-arguments-tp16095584p16203291.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.



reply via email to

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