anonymous functions and name resolution

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

anonymous functions and name resolution

Mike Miller
Hi,

In investigating bug #43237, I found the following strange name
resolution behavior in Octave:

  >> x
  error 'x' undefined near line 1 column 1
  >> func = @(x) x(:);
  >> func ([1 2])
  ans =
     1
     2

So far so good. Now I introduce a function file named x:

  >> rename xfunc.m x.m;
  >> x
  ans =  3.1416
  >> func ([1 2])
  ans =
     1
     2
  >> func = @(x) x(:);
  >> func ([1 2])
  ans =  3.1416

And one more definition of x to confuse things further:

  >> x = 12;
  >> x
  ans =  12
  >> func = @(x) x(:);
  >> func ([1 2])
  ans =  3.1416

So normally, a workspace variable takes precedence over a function, but
in the body of an anonymous function, the name is looked up as a
function instead.

Bug or subtle interaction of scope and precedence?

--
mike

Reply | Threaded
Open this post in threaded view
|

Re: anonymous functions and name resolution

bpabbott
Administrator

On Sep 24, 2014, at 9:37 PM, Mike Miller <[hidden email]> wrote:

> Hi,
>
> In investigating bug #43237, I found the following strange name
> resolution behavior in Octave:
>
>>> x
>  error 'x' undefined near line 1 column 1
>>> func = @(x) x(:);
>>> func ([1 2])
>  ans =
>     1
>     2
>
> So far so good. Now I introduce a function file named x:
>
>>> rename xfunc.m x.m;
>>> x
>  ans =  3.1416
>>> func ([1 2])
>  ans =
>     1
>     2
>>> func = @(x) x(:);
>>> func ([1 2])
>  ans =  3.1416
>
> And one more definition of x to confuse things further:
>
>>> x = 12;
>>> x
>  ans =  12
>>> func = @(x) x(:);
>>> func ([1 2])
>  ans =  3.1416
>
> So normally, a workspace variable takes precedence over a function, but
> in the body of an anonymous function, the name is looked up as a
> function instead.
>
> Bug or subtle interaction of scope and precedence?

Your example works as you'd expect in Matlab.  Also if "fun = @(x) x;", then Octave works as expected.

Looks like a bug.

Ben



Reply | Threaded
Open this post in threaded view
|

Re: anonymous functions and name resolution

Mike Miller
On Thu, Sep 25, 2014 at 07:56:37 -0400, Ben Abbott wrote:

>
> On Sep 24, 2014, at 9:37 PM, Mike Miller <[hidden email]> wrote:
>
> > Hi,
> >
> > In investigating bug #43237, I found the following strange name
> > resolution behavior in Octave:
> >
> >>> x
> >  error 'x' undefined near line 1 column 1
> >>> func = @(x) x(:);
> >>> func ([1 2])
> >  ans =
> >     1
> >     2
> >
> > So far so good. Now I introduce a function file named x:
> >
> >>> rename xfunc.m x.m;
> >>> x
> >  ans =  3.1416
> >>> func ([1 2])
> >  ans =
> >     1
> >     2
> >>> func = @(x) x(:);
> >>> func ([1 2])
> >  ans =  3.1416
> >
> > And one more definition of x to confuse things further:
> >
> >>> x = 12;
> >>> x
> >  ans =  12
> >>> func = @(x) x(:);
> >>> func ([1 2])
> >  ans =  3.1416
> >
> > So normally, a workspace variable takes precedence over a function, but
> > in the body of an anonymous function, the name is looked up as a
> > function instead.
> >
> > Bug or subtle interaction of scope and precedence?
>
> Your example works as you'd expect in Matlab.  Also if "fun = @(x) x;", then Octave works as expected.
>
> Looks like a bug.

Thanks for confirming, reported as http://savannah.gnu.org/bugs/?43299.

--
mike