Copies the lexical closure of a procedure
MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
copyClosure(f) copies the lexical closure
of a procedure or procedure environment
Usually, when a procedure is copied, for example by assigning it to an identifier, the lexical closure of the procedure is not copied. Via the copied procedure one can change the lexical closure of the original procedure. Thus, the lexical closure of a procedure shows the so-called reference effect.
copyClosure may be used to copy the lexical
closure of a procedure. Changes in the closure of the copy no longer
affect the original procedure's closure.
Closures are implemented by procedure environments (kernel type
copyClosure works by copying all lexically
enclosing procedure environments of a procedure.
copyClosure may also be used to copy a procedure
environment and all its lexically enclosing environments only.
Procedure closures show the reference effect: The procedure
gen changes its closure via the variable
A “normal” copy
the variable in the same closure, as is seen by repeatedly calling
gen:= proc() option escape; local i; begin i := 0; proc() begin i := i+1 end end:
f := gen(): g := f: f(), g(), f(), g()
If one now generates
f again by calling
g by calling
g has its own closure and now longer changes
i in the closure of
f := gen(): g := copyClosure(f): f(), g(), f(), g()
A procedure or procedure environment to be copied
Copied procedure or procedure environment