[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Default arguments
From: |
Søren Hauberg |
Subject: |
Default arguments |
Date: |
Thu, 14 Dec 2006 10:22:42 +0100 |
User-agent: |
Thunderbird 1.5.0.8 (X11/20061115) |
Hi,
I'd really like Octave to have some way of having default function
arguments. In C++ you can do
void fn(int arg = 7) {}
I think R supports the same syntax. I don't think matlab has something
similar, so if Octave adds support for default it should be something
simple that won't break whatever syntax matlab decides to add in the
future that'll handle default arguments.
So I suggest that Octave just adds some functions to make it easy to
work with default arguments, instead of changing the syntax of the
language. The C++ example above would then be written something like this
function fn(arg)
arg = default_value("arg", 7);
endfunction
The function default_value should then expand (like a macro in C/C++)
into this code
if (!exist(arg, "var")
arg = val;
endif
One thing that's missing from this idea, is a way to tell the function
to use the default argument. As an example, think of a function that
takes three arguments
function fn2(arg1, arg2, arg3)
...
endfunction
How does the user tell fn2 that he wants to set the value of arg1 and
arg3, but use the default value for arg2? I suggest adding a function
called "default" that returns an object of type "default". That way the
user can write
fn2(arg1, default, arg3)
I don't know how to handle varargin, but I don't think there is much one
can do...
Søren