Re: Closures behaviour

"John W. Eaton" <[email protected]> Fri, 26 Feb 2021 13:58:47 -0500
Newsgroups gmane.comp.gnu.octave.maintainers
Message-ID <[email protected]>
On 2/26/21 12:43 PM, CdeMills wrote:
> Hello,
> 
> I had to revive some old code. Since I use more and more Python, I wanted to
> use closure mechanisms to keep the computation cleaner.
> 
> So I started refactoring the code, creating a main function which returns a
> struct with a few function pointers. Those function pointers are initialized
> to sub-funcs of the main function. According to MatLAB doc, the behavior is
> that accessing the variable name in the subfunc has the effect of reading
> the actual value from the parent environment.
> 
> I tested a very small example from
> https://research.wmz.ninja/articles/2017/05/closures-in-matlab.html which
> works in Octave 6.2. Now, on my code, I noticed that only a small number of
> variables from the parent is accessible from the innermost context. The most
> basic symptom is that the number of variables returned by "who" shrinks in
> the internal functions.
> 
> I also tried under MatLAB 2018b, and all variables initialized before the
> first function definition in the parent function are shared with all its
> children.
>   
> Is this some well-known behavior which will be aligned on MatLAB in the long
> run ?

Handles to nested functions are expected to work in a way that is 
compatible with Matlab.  So if they don't, please file a bug report with 
sufficient information for someone to reproduce the problem.

jwe