[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Expected behavior for variables which shadow function names
From: |
Rik |
Subject: |
Re: Expected behavior for variables which shadow function names |
Date: |
Thu, 07 Mar 2013 11:04:35 -0800 |
On 03/07/2013 10:55 AM, John W. Eaton wrote:
> On 03/07/2013 01: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 think that in the olden times, Matlab behaved the same was as Octave in
> this case. But now, Matlab does more checking to ensure that you are not
> using symbols as both variables and functions within the same scope, or
> that you are not trying to reuse a cleared variable as a function, etc.
> I think the newer behavior is better. Maybe we should fix Octave to
> perform similar checks now that compatibility with the old and relatively
> confusing behavior is no longer an issue for us.
John,
I would definitely prefer that. Can someone verify on a recent version of
Matlab how this behaves? If they have upgraded their behavior then we can too.
--Rik