33
votes
Accepted
Is the renaming mechanism of `With` flawed?
Short answer
The local variables of the form varname$... are used by the system, and it is unwise to use symbols with such names as local variables.
...
28
votes
Should I always be using Module in functions unless I explicitly want global variables?
The short answer is "yes", we should always use Module to localize any intermediate variables within our functions.
We might get away with using global variables ...
26
votes
SetOptions locally?
Version 12.2
In version 12.2, use the new function WithCleanup[]:
...
Community wiki
26
votes
Accepted
What causes Module variables to leak?
Preamble
I will try to summarize some cases I've seen or encountered, in a few rules, which I believe do hold and explain most or all of the cases I am aware of.
The rules
Here are the rules (...
23
votes
Bug with iterated With: Referring to function arguments invalidates previous definitions
A long comment:
Automatic renaming again:
ClearAll[F];
F[x_] := Hold[With[{a = x}, {b = x}, a]];
F[0]
...

Kuba♦
- 136k
20
votes
Accepted
Why does the global variable exist here, where it was declared only in Module?
Why is this happening
The explanation was basically given by ciao in comments. You can also find a lot of information on this in this great answer of Mr.Wizard. I will perhaps try to view it from a ...
19
votes
Bug with Block & Lookup: Scope Variable Leak
Looks like a bug to me as well. Just as Michael I would suspect that the problem is more with Lookup than Block or ...
17
votes
Accepted
Working with DynamicModule: SetDelayed + OwnValues
J. Fultz' answer indicates that it is not a bug but a feature which needs better documentation.
Get a habit of defining your procedures with [], e.g. ...

Kuba♦
- 136k
17
votes
SetOptions locally?
I have used several methods, including the ones in the other answers. I have found that the simplest method is Internal`InheritedBlock as it allows for temporary ...
17
votes
Accepted
Why can't I inject expressions in Compile using (only) With
Compile is considered a scoping construct by the outer With, and its bindings are protected. This will work:
...
16
votes
Accepted
Parallelization inside a module: Proper use of local variables
There are multiple issues here. Let us take them one by one.
Why do you get 0 as the output?
There is a quirk in how ...
15
votes
What does empty {} mean in Module?
Usually, when one defines a function that's not too complex (usually a one-liner) it is customary (here we mean Mathematica custom) to define it directly without any scoping constructs (...
15
votes
Accepted
Internal DynamicModule steals scope of external DynamicModule
This answer builds on @AlbertRetey's answer where, in the comments, you wanted some guidance as to how Wolfram creates robust controls.
Think very carefully about the division of labor between the FE ...
15
votes
Accepted
Rule rewriting behavior
Overview (tl;dr)
Ultimately, the difference here is due to Module being lexically scoped, while Block is dynamically scoped.
...
14
votes
Is this buggy behavoir of Module solved?
IMHO, this is not a bug, but a feature. It allows you to implement pointer-like data structures with garbage collection because symbols in a Module have the ...
13
votes
Accepted
Where does a package have to be loaded?
tl;dr
You need to call Needs before GetBoundaryMesh definition so it can be parsed (found in correct context) correctly or you ...

Kuba♦
- 136k
13
votes
RecurrenceTable iteration variable not localized? Bug or user error?
As Bob Hanlon's answer points out, RecurrenceTable does not hold its arguments, but most especially, it does not hold its iterator arguments. This must surely be ...
13
votes
Accepted
What is the accessibility of a package's `Private` context variables?
So how does MyFunction know the value of abc at the delayed function call (when it is called) if the ...
12
votes
Bug: With[] pattern-variable renaming misses symbols within `Except`
I agree that this is a bug. However, I want to point out that this usage of Except does not seem to be allowed in older versions.
In version 9.0:
We don't get the ...
12
votes
12
votes
Accepted
OptionValue doesn't work if a local variable has the same name as the function itself
Looks like a variable capture, caused by the macro-like expansion mechanism of OptionValue , which happens before the r.h.s. of the function gets evaluated.
What ...
12
votes
Accepted
Garbage collection for lexical closures
Here's an approach to "automate" the usage of the ExpressionCleanup` paclet mentioned by @LeonidShifrin. The idea is to return an object from your module ...
11
votes
Accepted
How does $CellContext` work?
And if there is a possibility to manually use it for something useful?
Yes, it solves a problem I had many times and which I had to work around. And none of those work arounds was as general as a ...

Kuba♦
- 136k
11
votes
Accepted
Blocking Dataset`$ElisionThreshold doesn't work
The resulting Dataset expression is typeset by the front-end after evaluation is complete. This means that the Block expression ...
11
votes
Accepted
Import another Notebook as if it were a Package
You can temporarily change CellContext of the notebook with SetOptions or CurrentValue but ...

Kuba♦
- 136k
11
votes
Accepted
Package functions and symbolic calculations
Short explanation
It returns 0 because you are doing something like:
...

Kuba♦
- 136k
11
votes
RecurrenceTable iteration variable not localized? Bug or user error?
Unlike many other functions that use an iterator, RecurrenceTable does not have the attribute HoldAll. Presumably, this ...
11
votes
Accepted
Memory leak with FindMaximum/Minimum inside of Module
Please report this as a bug. A minimal example:
...
11
votes
Garbage collection for lexical closures
If you can afford this from performance standpoint, you can generate definitions at every function call:
...
10
votes
Internal DynamicModule steals scope of external DynamicModule
The cause is the variable name confusion. It is true that each DynamicModule creates different symbol names. However, a new symbol name is generated again in ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
scoping × 561function-construction × 67
dynamic × 52
manipulate × 51
functions × 47
evaluation × 46
variable-definitions × 37
programming × 33
plotting × 26
list-manipulation × 25
packages × 22
replacement × 20
core-language × 20
bugs × 17
compile × 17
contexts × 17
performance-tuning × 16
assignment × 16
differential-equations × 15
variable × 15
pattern-matching × 14
equation-solving × 13
pure-function × 13
memory × 12
hold × 12