octave-maintainers
[Top][All Lists]
Advanced

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

Re: anonymous functions and name resolution


From: Ben Abbott
Subject: Re: anonymous functions and name resolution
Date: Thu, 25 Sep 2014 07:56:37 -0400

On Sep 24, 2014, at 9:37 PM, Mike Miller <address@hidden> 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 via email to

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