octave-maintainers
[Top][All Lists]
Advanced

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

Re: proposed parser changes


From: John W. Eaton
Subject: Re: proposed parser changes
Date: Thu, 20 Mar 2008 16:30:12 -0400

On 20-Mar-2008, Wolfgang_M wrote:

| Given the function 'foo' which is defined like:
| ****
| function result=foo( vect, mean)
|       result=(mean -vect)
| 
| end %end of function
| ****
| 
| Called with only one argument, the variable 'mean' will be treated as a
| function with the negative
| argument 'vect'.

No, that should not happen.  First, because mean is named in the
argument list, it is a variable in the function even if foo is only
called with one argument.  Also, if you rewrite your function as

  function foo (vect)
    result=(mean -vect)
  end

so that mean must be a function, then this should still not call mean
with the argument '-vect' because the command syntax should only be
recognized at the beginning of a statement.

| In this case with the matlab-function 'mean', it is clear, what happens and
| you might find
| this constellation. But if you have a similar function 'foo( vect, srcBar)'
| and 'srcBar' is the name of a function on my machine in the matlab path but
| not at your computer,
| you will never be able to reconstruct the wrong results I could get with
| this function.

No, because any variable named in the argument list is treated as a
variable inside the function, independent of whether you pass a value
for the argument when you call the function.

| Furthermore, if you work with objects (maybe in octave 3.1?)  you might
| often use
| function names as variables.

If you mean by using function handles, then I think that's a separate
issue.  Also, you can't call a function through a function handle with
the command syntax.  For that, you must use the function call syntax.

| By the way another case which is already making trouble in matlab:
| 
| Try in R2007a:
| 
| mean-1
| mean -1

Yes, this is well known, and apparently doesn't cause too much
trouble.

| (second comand with whitespace between 'mean' and '-1')
| the first command throws an error (like expected), but the second one gives
| out '47' on my computer.
| I dont know whats happening there,

With the command syntax, the function is passed the arguments as
character strings.  So

  mean -1

is equivalent to

  mean ('-1')

and in Matlab, '-1' in a numeric context is equivalent to the numeric array
[45, 49] (i.e., the decimal ASCII values of the characters in the
character array).

| in matlab,
| it might not be an advantage, if also octave does similar things.

It is an advantage to provide compatible behavior, and I think many
people coming to Octave from Matlab would like to be able to define
functions that can also be called with the command syntax without
having to declare them specially (by using the mark_as_command
function in Octave).

We already provide the possibility for functions to be declared as
commands, so the sort of confusion caused by "mean -1" can already
happen[1].  The question is whether we should allow this for all
functions or just those that have been specially tagged.

jwe

[1] However, Octave's mean function rejects character string arguments
    and I don't think anyone has complained, so I guess calculating
    mean values of the ASCII values of character strings is not a
    widely used feature.


reply via email to

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