octave-maintainers
[Top][All Lists]
Advanced

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

Re: Default arguments


From: Søren Hauberg
Subject: Re: Default arguments
Date: Fri, 15 Dec 2006 09:15:31 +0100
User-agent: Thunderbird 1.5.0.8 (X11/20061115)

John W. Eaton skrev:
Wouldn't it be better to have the syntax

  function foo (arg = default_val)
Oh yes, very much! I just figured it would be harder to add such a change...

As you note, there is the possiblity of a future incompatibility with
Matlab.  Is this feature worth the risk?  I think I would want to use
it in functions that are distributed with Octave to simplify code that
needs to set default values.
As Tom noted, matlab will probably use the same syntax in the future (it is the defacto standard), so I think it's okay.

| One thing that's missing from this idea, is a way to tell the function | to use the default argument.
My patch overloads ":" for this purpose:
Is that reasonable?
I'm not sure. I think ":" is the obvious choice, but I do have one issue. Since matlab has decided to use "( )" to embreace function arguments AND index matrices it is hard to tell functions and matrices apart. One can think of functions as generalized matrices, so to some extend this choice is okay (I'm not a fan though). However, if we allow ":" as an argument to a function it might be even harder to tell matrices and functions apart.

Or, maybe what you really want is keyword arguments?  I think that
would be harder to add.
Well, those would be nicer so you can avoid writing
  my_function(:,:,:,:,:,:, my_value)
which can be hard to read and instead write
  my_function(my_arg = my_value)
My original proposal was to just do something that was easy to implement, so I think it would be okay to not have keyword arguments. Also, I don't think the ":" operator conflicts with keyword arguments, so that could be added later on.

Although I've checked this patch in to the CVS archive, I consider it
to be quite experimental.  If any nontrivial problems show up that are
related to this change, I'll simply remove it.
I can't really test this until sometime next week (I have a hard deadline on monday), but then I'll report back...

Thanks,
Søren

P.S. Sometimes your abilities as a coding-ninja really impresses me...


reply via email to

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