On Sat, 30 Jul 2011, Allin Cottrell wrote:
On Fri, 22 Jul 2011, Sven Schreiber wrote:
>
> It's nice to be able to omit parameters in function calls when they are
> redundant. However, this would seem to apply to all types of arguments.
> I somehow would prefer a coherent solution to this instead of a
> case-by-case approach (which is hard to remember).
I'm not sure what you have in mind. The way I see it, the
suitable form of a "don't care" default for a given trailing
parameter depends on its type. "null" is fine for pointer
types but otherwise does not make sense (at least for people
who tend to think in C). For booleans, the only sane default
would be 1 or 0; and so on.
In the end, it's up to the function writer to handle the
arguments received, and the writer has the discretion to set a
default that will be interpreted inside the function as "don't
bother with this", where applicable. For example, with an
optional parameter whose valid values are non-negative ints, a
default of -1 could be used.
It happened to me several times in the past to long for a mechanism for
dealing with optional parameters like the one R has. For those unfamiliar
with R: when you call a function, you can specify in the function call a
non-default value only for the arguments you need, as in "foo(b=3)" or
"foo(a=2, b=3)". I think this is very smooth and elegant. When you write
functions that have to take lots of arguments, the C-like convention we
have (which is actually more lenient than C, in which you don't have
defaults at all) may be awkward. However, IMO this small difficulty can be
circumented very easily if you use bundles: instead of defining a function
with 12 parameters, just define it as a function of a bundle which
contains them, and then handle the unpacking (which is very economical,
CPU-wise) inside the function itself. In my experience, this also leads
you to writing much cleaner code.
Riccardo (Jack) Lucchetti
Dipartimento di Economia
Università Politecnica delle Marche
r.lucchetti(a)univpm.it
http://www.econ.univpm.it/lucchetti