Hot answers tagged

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. ...
Leonid Shifrin's user avatar
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 ...
WReach's user avatar
  • 68.4k
26 votes

SetOptions locally?

Version 12.2 In version 12.2, use the new function WithCleanup[]: ...
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 (...
Leonid Shifrin's user avatar
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's user avatar
  • 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 ...
Leonid Shifrin's user avatar
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 ...
Albert Retey's user avatar
  • 23.5k
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's user avatar
  • 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 ...
rcollyer's user avatar
  • 33.9k
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: ...
Leonid Shifrin's user avatar
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 ...
Szabolcs's user avatar
  • 233k
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 (...
RunnyKine's user avatar
  • 32.9k
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 ...
John Fultz's user avatar
  • 12.6k
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. ...
thorimur's user avatar
  • 8,980
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 ...
Henrik Schumacher's user avatar
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's user avatar
  • 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 ...
WReach's user avatar
  • 68.4k
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 ...
Szabolcs's user avatar
  • 233k
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 ...
Szabolcs's user avatar
  • 233k
12 votes

SetOptions locally?

Just define your options as sequence held in a local variable. ...
m_goldberg's user avatar
  • 107k
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 ...
Leonid Shifrin's user avatar
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 ...
Lukas Lang's user avatar
  • 33.5k
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's user avatar
  • 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 ...
WReach's user avatar
  • 68.4k
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's user avatar
  • 136k
11 votes
Accepted

Package functions and symbolic calculations

Short explanation It returns 0 because you are doing something like: ...
Kuba's user avatar
  • 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 ...
Bob Hanlon's user avatar
  • 152k
11 votes
Accepted

Memory leak with FindMaximum/Minimum inside of Module

Please report this as a bug. A minimal example: ...
Carl Woll's user avatar
  • 130k
11 votes

Garbage collection for lexical closures

If you can afford this from performance standpoint, you can generate definitions at every function call: ...
Leonid Shifrin's user avatar
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 ...
JungHwan Min's user avatar
  • 4,644

Only top scored, non community-wiki answers of a minimum length are eligible