|
From: | John W. Eaton |
Subject: | Re: Expected behavior for variables which shadow function names |
Date: | Thu, 07 Mar 2013 13:55:19 -0500 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:10.0.11) Gecko/20121122 Icedove/10.0.11 |
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.
jwe
[Prev in Thread] | Current Thread | [Next in Thread] |