octave-maintainers
[Top][All Lists]
Advanced

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

Re: Expected behavior for variables which shadow function names


From: Daniel J Sebald
Subject: Re: Expected behavior for variables which shadow function names
Date: Thu, 07 Mar 2013 12:41:48 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111108 Fedora/3.1.16-1.fc14 Thunderbird/3.1.16

On 03/07/2013 12:18 PM, Rik wrote:
3/7/13

All,

I ran across an odd behavior and I wanted to know what other people thought
was "correct".

For the following function:

function y = tstflag (flag)
   y = flag;
endfunction

When called with any argument it behaves as expected.

x = tstflag (1)
x =  1
x = tstflag ([])
x = [](0x0)

When called with no arguments, however, the return value is expanded to the
output of the flag() function which is a 64x3 matrix.

x = tstflag ()
x =

    1   0   0
    1   1   1
    0   0   1
    0   0   0
    ...

I understand what is happening, either the flag() function is shadowed by
the local variable or it isn't, but I wonder what Matlab does for this
corner case?  I naively assumed that no input would be similar to a null
input ([]).  I can always get around this by assigning a default value to
flag in the function header.

I would make the same assumption. This is potentially problematic and, at minimum, confusing to someone not paying real close attention. I suppose this is all avoided if one programs the script file to check the number of input arguments. However, as you've shown it can lead to unexpected results for valid routines not much more complex than the example you gave.

It seems to me the proper behavior for tstflag() would be to issue an error message similar to the following where I've change the variable reference to "flig" instead of "flag":

octave:10> x = tstflag()
error: `flig' undefined near line 3 column 7
error: called from:
error:   /home/sebaldd/octave/tstflag.m at line 3, column 5

Dan


reply via email to

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