copyClosure
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
)
copyClosure(f)
copies the lexical closure
of a procedure or procedure environment f
.
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 DOM_PROC_ENV
)
in MuPAD®. 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 f
generated
by gen
changes its closure via the variable i
.
A “normal” copy g
of f
changes
the variable in the same closure, as is seen by repeatedly calling f
versus g
.
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 gen
,
but copies g
by calling copyClosure
,
then g
has its own closure and now longer changes
the variable i
in the closure of f
.
f := gen(): g := copyClosure(f): f(), g(), f(), g()
|
A procedure or procedure environment to be copied |
Copied procedure or procedure environment