[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: |
Tue, 18 Mar 2008 03:04:33 -0700 (PDT) |
Soren Hauberg wrote:
>
> This thread:
> http://www.nabble.com/Default-arguments-td7869282.html#a7869282 dates
> back to late 2006 where John added support for default arguments. So,
> its been there for some time, but its not used that much.
>
I played a bit with it and found inconsistencies with regard to R:
function hello (who = "World", closing = "!")
printf("Hello, %s %s\n", who, closing);
endfunction
octave> hello(who = "Me")
Hello, Me !
octave> hello(closing="and the rest")
Hello, and the rest ! <= 'who' was set; 'closing' takes default value
octave> hello(who = "Me", closing="and the rest")
Hello, Me and the rest
octave> hello(closing="and the rest", who = "Me")
Hello, and the rest Me <= arguments were permuted
would it be possible to have a strict mapping when using the named
parameters paradigm ?
octave> function test(varargin, flag=false)
parse error: syntax error, invalid parameter list
function test(flag=false, varargin)
printf('flag is %d\n', flag);
for indi=1:length(varargin), disp(varargin{indi}); endfor
endfunction
octave> test(2, 3, 4, flag=true)
flag is 2
3
4
1
would it be possible
1) to be able to declare named arguments AFTER varargin
2) to ensure the same strict mapping, i.e. unnamed parameters go to
varargin, named ones are matched against the list of named arguments, named
ones without matching being flagged as errors ?
The purpose would be to achieve something similar to:
meanemp<-function(n,...,dist="Uniforme") {
+
switch(dist,Normale=rnorm(n,...),Uniforme=runif(n,...),Exponentielle=rexp(n,...))
+}
meanemp(5, sd=.1, dist="Normale") % normal distrib, 5 samples, mean 0,
standard deviation .1
[1] 0.02443245 0.07669428 -0.11214011 -0.01001278 -0.17726786
meanemp(5, 1, dist="Normale") % normal distrib, 5 samples, mean 1, sd 1
[1] 2.725002 1.775500 2.354803 -0.623507 1.873243
notice that meanemp(5, dist="Normale", 1) and meanemp(dist="Normale", 5, 1)
are identical ways of calling this function
The mechanism is as follows:
meanemp has a normal parameter, 'n', varargins, '...', and a named argument,
'dist'. Named argument(s) matching the function definition is (are)
extracted from the list, whatever their position. Then normal parameters are
matched based upon their position. The rest becomes varargins, both unnamed
and named, which are passed to daughters functions. There, they are matched
based first upon their name, second upon their position.
My guess is that this respects to principle of least surprise, delivering
results as the programmer expect them.
Greetings
Pascal
--
View this message in context:
http://www.nabble.com/functions-with-%27named%27-arguments-tp16095584p16117614.html
Sent from the Octave - Maintainers mailing list archive at Nabble.com.
- functions with 'named' arguments, Dupuis, 2008/03/17
- Re: functions with 'named' arguments, Søren Hauberg, 2008/03/17
- Re: functions with 'named' arguments, Dupuis, 2008/03/18
- Re: functions with 'named' arguments, Søren Hauberg, 2008/03/18
- Re: functions with 'named' arguments,
Dupuis <=
- Re: functions with 'named' arguments, Jaroslav Hajek, 2008/03/18
- Re: functions with 'named' arguments, Dupuis, 2008/03/18
- Re: functions with 'named' arguments, Shai Ayal, 2008/03/18
- Re: functions with 'named' arguments, Jaroslav Hajek, 2008/03/18
- Re: functions with 'named' arguments, Dupuis, 2008/03/18
- Re: functions with 'named' arguments, Jaroslav Hajek, 2008/03/18
- Re: functions with 'named' arguments, Dupuis, 2008/03/19
- Re: functions with 'named' arguments, Shai Ayal, 2008/03/19
- Re: functions with 'named' arguments, Jaroslav Hajek, 2008/03/19
- Re: functions with 'named' arguments, John W. Eaton, 2008/03/19