octave-maintainers
[Top][All Lists]
Advanced

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

functions with 'named' arguments


From: Dupuis
Subject: functions with 'named' arguments
Date: Mon, 17 Mar 2008 09:19:46 -0700 (PDT)

There was a recent discussion about evolutions for Octave 3.1. May I suggest
the following one ? But before, the context. High level programming
sometimes requires to write functions with various number of arguments. In
C, there are constructs for this, but you have to be very carefull about the
type of arguments. Try a 'printf("%d\n", 1.23) and enjoy. Object-oriented
languages introduce polymorphism, i.e. the ability to have one function with
many 'signatures', that is using the same name, but differenciating over the
number and type of arguments. The deal is that the right implementation is
called, and this is decided either at compile or run-time.

Now let's turn to a simple problem: a function requires a variable number of
vector or matrices, and some optionnal flags. With the varagin construct,
you can loop over arguments, and taking actions according to their types.
Matrices are concatenated in a big matrix, and scalars should be used as
flags. The first one has this meaning, the second has that meaning, ... But
wait. optionnal means they should take default values if none is provided,
their purpose is to fine-tune the algorithm inner working. So far so good,
but how to modify only some of the optionnal arguments ? 

I see two ways. In Perl, interfaces like DBI accept hashes as input, so some
call may looks like 
 $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit
=> 0 });
The last part is a hash, specifying some optionnal args and their value.
>From the programmer's perspective, a simple interation on the keys of the
hash is enough to scan those args. In R, arguments specification contains 3
parts: "usual" args, i.e. without default values, "optionnal", and "named".
Named arguments is a special type, they are optionnal with default value.
F.i, the prototype of "mean" is
mean(x, trim = 0, na.rm = FALSE, ...). The 3 sorts are present, and the
default for the named ones is defined at the prototype.

Would itbe possible to implement one of those mechanisms in Octave ? The
problem I don't see a solution at present is passing multiple optionnal args
of the same type, with different roles, in a free-form fashion.

Regards

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



reply via email to

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