Hot answers tagged

47 votes

How to avoid nested With[]?

Introduced in V10.4 or earlier, but after V10.1 This functionality has snuck into With (ref: Daniel's comment). Note the use of the braces. ...
user avatar
  • 216k
35 votes
Accepted

What is the purpose of Internal`LocalizedBlock?

Internal`LocalizedBlock behaves the same as Block, but it can localize non-Symbols (e.g. f[1]...
user avatar
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. ...
user avatar
27 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 ...
user avatar
  • 67k
25 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 (...
user avatar
24 votes

SetOptions locally?

Version 12.2 In version 12.2, use the new function WithCleanup[]: ...
23 votes
Accepted

Strange behavior with Cases and Position with SetDelayed

What's happening This is not simple by any means. You have encountered another instance of a general situation with lexical scope leaks / emulation / over-protection by symbol renaming. The case at ...
user avatar
23 votes
Accepted

Do Table iteration variables need to be localized using Module?

In general, it is good practice to include i among the local variables. Table does not localize its variable (or, as some say, ...
user avatar
  • 67k
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] ...
user avatar
  • 133k
21 votes
Accepted

StringReplace, ReplaceAll and Rule interact in a bizarre way

Preamble What happens can be understood when we recall that Rule is a scoping construct. The general issues related to variable renamings in scoping constructs ...
user avatar
21 votes
Accepted

Syntax highlighter shows error for Manipulate when it's inside a Block or a Module

Based on Mr.Wizard's answer and comments by Szabolcs and celtschk, I now understand that the code I posted does have undesirable side-effects and it should be avoided. Specifically, the scoping ...
user avatar
21 votes
Accepted

I define a variable as local to a module BUT then the module uses its global value! Why?

What happens and why As Daniel Lichtblau pointed out in comments, this behavior can also be viewed as a flaw in the current behavior / design / implementation of lexical scoping in Mathematica. ...
user avatar
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 ...
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 ...
user avatar
  • 23.1k
17 votes
Accepted

Context of localised (dynamic) symbols

I will make no attempt to defend the fact that Mathematica simulates scoping by means of variable renaming. However, the behaviour that we see is consistent with the principles under which ...
user avatar
  • 67k
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 ...
user avatar
  • 33.3k
16 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. ...
user avatar
  • 133k
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 ...
user avatar
  • 225k
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 (...
user avatar
  • 32.5k
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 ...
user avatar
  • 12.4k
15 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: ...
user avatar
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. ...
user avatar
  • 8,208
14 votes
Accepted

A smarter nested With?

With this helper function: ...
user avatar
14 votes
Accepted

A With expression local variable specification without head List

This is a documentation bug. Our source notebooks do contain documentation for experimental and future features, but they are supposed to be stripped in the process of being bundled with the product. ...
user avatar
  • 13.4k
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 ...
user avatar
  • 133k
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 ...
user avatar
  • 67k
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 ...
user avatar
  • 225k
12 votes

What is the purpose of Internal`LocalizedBlock?

Disclaimer: these are all guesses, believe at your own risk. As the first argument, you specify a list of patterns to localize. For example, {x}, ...
user avatar
  • 41.6k
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 ...
user avatar
  • 225k
12 votes

SetOptions locally?

Just define your options as sequence held in a local variable. ...
user avatar
  • 106k

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