octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #59989] Wrong scope of nested function in anon


From: Rik
Subject: [Octave-bug-tracker] [bug #59989] Wrong scope of nested function in anonymous function handle
Date: Tue, 2 Feb 2021 11:19:35 -0500 (EST)
User-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36

Follow-up Comment #1, bug #59989 (project octave):

This code is making use of a feature which is relatively obscure and which
certainly doesn't get a lot of testing.  Nested functions within a larger
function are supposed to have access to the namespace of the enclosing
function.  Hence, it is possible to define the equivalent of a pseudo-global
variable that is seen by all nested functions without having to be explicitly
passed as a parameter.

This should be fixed in Octave, but as a point of coding it is usually better
to pass data to a function for processing.  In software architecture this
allows the code to be modular and to guarantee that a function can only
modify, potentially, inputs passed to it and have no other side effects.  With
global variables, it can be very difficult because any code, any where could
potentially change the value.

One possible justification for not re-coding is if the variable is very large
such that passing it down to each function would be a performance hit.  The
notes in the code say that the variable 'a' is hard to calculate, but don't
say whether it is large.  If it just takes a lot of code to calculate a single
constant value then it would be better to pass 'a' down.  If 'a' is a large
matrix then this does not apply.

Here is a simplified function which exhibits the same behavior.


function retval = tst_nestfcn ()
  x = pi;

  function y = nestfcn1 ()
    y = x;
  endfunction

  function y = nestfcn2 (h)
    y = h();
  endfunction
 
  retval = nestfcn2 (@nestfcn1);        # WORKS
  retval = nestfcn2 (@() nestfcn1());   # FAILS

endfunction




    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?59989>

_______________________________________________
  Message sent via Savannah
  https://savannah.gnu.org/




reply via email to

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