octave-maintainers
[Top][All Lists]
Advanced

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

Re: polymorphism in functions


From: Paul Kienzle
Subject: Re: polymorphism in functions
Date: Thu, 8 Jan 2004 22:47:16 -0500


On Jan 8, 2004, at 11:19 AM, John W. Eaton wrote:

On 8-Jan-2004, Pascal A. Dupuis <address@hidden> wrote:

| One possible solution is to use a formalism of named arguments, like
| in R:

This type of feature has been discussed a number of times in the last
10 years, but so far no one has done the work to implement it.

I'm sure I've mentioned this before, but what about a syntax
for structure constants?  Then you can put all your named
parameters into a structure, and write a simple routine which
takes a structure of default values and overrides it with the
values in the user supplied structure.

E.g.,

result=myfunc(x, {threshhold-> [.1:.1:.9], expected-> ones(1,10)*length(x)/10})

You can even do it now without the syntactic sugar:

result=myfunc(x,struct('threshhold',[.1:.1:.9],'expected',ones(1,10)*len gth(x)/10))

though the usual matlab style is to drop the struct and just use:

result=myfunc(x,'threshhold',[.1:.1:.9],'expected',ones(1,10)*length(x)/ 10)

In myfunc you need a statement to define the defaults, and another to process
the args.  E.g.,

        function x = myfunc(x, varargin)
static def=struct('threshhold',linespace(0,1,25),'expected',ones(1,25),'verbose ','off');
        opts=options(def,varargin);
        ...
        t  = opts.threshhold;
        ...
        endfunction

Octave-forge has read_options for processing these, and I have my own
variant.  Neither routine has the right interface IMHO, so feel free to
propose something better.

For example, it might be nice to use a structure array, with element 1
giving the  default value and element 2 giving type information so that
we can do more checking automatically.  Values we don't want checked
have no type.

Positional parameters could be specified as well, using special field
names  like def._1x for the first positional parameter, whose value is
to be stored in opts.x.

Boolean values could be set as 'opt','on'/'opt','off', or '+opt'/'-opt', or 'opt'/'noopt' (the first for compatibility, the last because it is the most
natural, and the second just because we can).

Instead of returning a structure, we could use assignin('caller','x',value).
Then instead of opts.x we can use x directly in our routine.

Of course we won't be able to satisfy the requirements for all
functions without making this scheme far too complicated, but
if we can handle 90% of the checks easily then we will be better off.

Paul Kienzle
address@hidden



reply via email to

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