Skip to main content

All Questions

Tagged with or
Filter by
Sorted by
Tagged with
260 votes
9 answers
19k views

What are the use cases for different scoping constructs?

Mathematica includes three functions that I know of which can be used to effectively (if not actually) define a variable and give it a value within a local scope: ...
David Z's user avatar
  • 4,961
90 votes
6 answers
4k views

How to avoid nested With[]?

With[ {v1 = #}, With[ {v2 = f[v1]}, g[v1, v2] ] ] How to avoid nested With[] like the above? I'd like to use <...
user13253's user avatar
  • 8,674
57 votes
2 answers
2k views

How safe is the use of Block and Internal`InheritedBlock

For many questions on this site answers are given which suggest to use Block and, especially for more advanced topics, ...
Albert Retey's user avatar
  • 23.6k
52 votes
1 answer
2k views

Local variables in Module leak into the Global context

From the Mathematica documentation on Module: Module allows you to set up local variables with names that are local to the module. But after using ...
owler's user avatar
  • 623
51 votes
5 answers
3k views

Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs

Using global variables the following turns an "expression" into a Function: expr = 2 x; Function[x, Evaluate[expr]] Of course ...
einbandi's user avatar
  • 4,064
50 votes
1 answer
1k views

What does Internal`InheritedBlock do?

What does the function Internal`InheritedBlock do? How is it different from the regular Block?
Ted Ersek's user avatar
  • 7,164
42 votes
4 answers
3k views

Why modules with no variables?

I was reading some code, in particular, recipe 4.13 on unification pattern-matching in Sal Mangano's Mathematica Cookbook, and there were many instances of Modules with no variables in them, such as ...
Reb.Cabin's user avatar
  • 8,763
33 votes
2 answers
3k views

What are some advanced uses for Block?

I read the answers to this question (What are the use cases for different scoping constructs?) and this one (Condition, Block, Module - which way is the most memory and computationally efficient?). ...
VF1's user avatar
  • 4,742
32 votes
2 answers
943 views

What causes Module variables to leak?

Sometimes, after writing code like f[] := Module[{a}, ...] and running f[] multiple times, there will be many ...
Szabolcs's user avatar
  • 236k
32 votes
1 answer
964 views

Is the renaming mechanism of `With` flawed?

I'm confused about the renaming mechanism of With: ...
luyuwuli's user avatar
  • 2,814
31 votes
1 answer
11k views

Module with "local functions"

I am trying to use a Module having "local functions", i.e., those which I need to define only inside this module. So I tried this: ...
Pavithran Iyer's user avatar
29 votes
4 answers
1k views

SetOptions locally?

Is there a way to SetOptions locally? For example, I have a Module and inside it I am doing a lot of ...
a06e's user avatar
  • 11.5k
29 votes
2 answers
835 views

What is the purpose of Internal`LocalizedBlock?

I have come across the (internal) use of the function: Internal`LocalizedBlock I am trying to determine its purpose. It seem to behave like ...
Mr.Wizard's user avatar
  • 273k
29 votes
1 answer
445 views

Strange behavior with Cases and Position with SetDelayed

I feel like I must be missing something simple and obvious here, but this has me scratching my head. This works as expected: ...
rhennigan's user avatar
  • 1,783
26 votes
1 answer
514 views

Bug with Block & Lookup: Scope Variable Leak

After debugging a large chunk of code, I could identify this MWE. ...
Murta's user avatar
  • 26.4k
26 votes
0 answers
537 views

Compile not correctly initializing a variable defined inside Module

Bug introduced in 8.0, fixed in 10.4, intentionally reintroduced in 11.0 and persisting through 13.0.0 or later In the following example, inside the minimumX's <...
P. Fonseca's user avatar
  • 6,715
23 votes
3 answers
1k views

Nested definition: How can I define a function with a passed-in expression?

Here's a simplified version of what I'm trying to do: ...
celtschk's user avatar
  • 19.2k
23 votes
1 answer
481 views

Bug with iterated With: Referring to function arguments invalidates previous definitions

Thank you for your report. I have forwarded your report to the appropriate team for review. - WRI ...
masterxilo's user avatar
  • 5,779
22 votes
2 answers
910 views

Should I always be using Module in functions unless I explicitly want global variables?

I define functions a ton. In the past, I've mostly just defined my functions like: myFunc[var1_]:=( var2=var1^2; Return@var2; ) And I haven't run into too ...
YungHummmma's user avatar
  • 3,102
22 votes
2 answers
1k views

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

I have a question about modules and local variables. Here's my example: h = 5; Module[{a, h}, a[h_]= h^2; a[4]] (*Out[2] = 25*) I expected the module to ...
pan_e_gaban's user avatar
22 votes
2 answers
1k views

Module trash collection behaviour

I have asked this question before on the Wolfram community forum (http://community.wolfram.com/groups/-/m/t/83845?p_p_auth=Gxoxe65B) but not with great success. Module creates temporary variables of ...
TimRias's user avatar
  • 3,302
21 votes
3 answers
2k views

Variable scope similar to other languages

Mathematica, unlike many other languages, defines the variables in the global scope unless it is explicitly asked to define a variable in a local scope, e.g., using ...
Helium's user avatar
  • 4,069
21 votes
1 answer
288 views

"With" as part of the "Dynamic Interactivity Language"

I noticed that in Mathematica's Function Navigator With is listed as being part of the "Dynamic Interactivity Language". It is not the first (or even the fourth, ...
Sjoerd C. de Vries's user avatar
21 votes
1 answer
286 views

Why are delayed definitions in scoping functions not documented?

As outlined here, := may be used in scoping constructs like With to give delayed definitions, but this is undocumented. This ...
Mr.Wizard's user avatar
  • 273k
21 votes
1 answer
689 views

ClearAll::clloc - "Cannot clear local variable x" in Block[{x}, ...] - why?

Block[{x}, ClearAll@x] ClearAll::clloc: Cannot clear local variable x. The documentation just says An error occurs because ClearAll cannot be used with a ...
masterxilo's user avatar
  • 5,779
21 votes
1 answer
332 views

Integrate - Why is the integration variable not local

I have noticed something odd: Let f[t_]:=Integrate[x, {x, 0, t}] h[t_]:=Sum[x, {x, 0, t}] Now, enter ...
Lukas Lang's user avatar
  • 34.4k
20 votes
3 answers
1k views

How does $CellContext` work?

The only official info about $CellContext` I was able to find is: placeholder for the context of a symbol inside Dynamic ...
Kuba's user avatar
  • 138k
19 votes
4 answers
643 views

Internal DynamicModule steals scope of external DynamicModule

I have two dynamic objects, one nested in the other. Both have their own functions defined, as follows. I have simplified them to the point where they don't make much sense, but can still illustrate ...
István Zachar's user avatar
18 votes
2 answers
1k views

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

I'm putting my new copy of Mathematica 10 through its paces and I noticed a weird change in the colouring scheme that I find very annoying, and I'd like to get some insight into why it's happening. ...
Emilio Pisanty's user avatar
18 votes
2 answers
562 views

Garbage collection for lexical closures

Mathematica is known not to garbage collect temporary variables in certain cases. I am particularly interested in the very useful programming pattern known as a lexical closure. A somewhat trivial ...
user293787's user avatar
18 votes
1 answer
670 views

A With expression local variable specification without head List

I just noticed a new form of With in its documentation: With[$x=x_0$, $expr$] specifies that all occurrences of the symbol $x$ in $expr$ should be replaced ...
jkuczm's user avatar
  • 15.1k
17 votes
1 answer
8k views

Returning a value from a Module

In the following simple example MyMod := Module[{i}, Do[ Return[1], {i, 3} ]; Print["test"] ] I expected the module to exit on ...
enzotib's user avatar
  • 1,094
17 votes
3 answers
2k views

What is the right way to construct DynamicModule

When I'm creating DynamicModules for CDF purposes I'm usualy basing on my experience with that, particular goal and content rather than on deep insight. I want to ...
Kuba's user avatar
  • 138k
17 votes
1 answer
541 views

Is the sharing of variables in Module/Block within Compile documented behavior?

Today I noticed something, I think for the first time. When used inside Compile variable values within Module (and ...
Mr.Wizard's user avatar
  • 273k
17 votes
1 answer
564 views

Modules that initialize themselves on first call

I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
George Wolfe's user avatar
  • 5,482
17 votes
2 answers
433 views

Messages leak out from Block

Bug introduced in 8.0 or earlier and fixed in 10.1 When assigning message to a symbol localized by Block, I would expect that this message is not accessible ...
jkuczm's user avatar
  • 15.1k
16 votes
4 answers
3k views

Subscripts - Why do I see the error "only assignments to symbols are allowed" when using a Module and not otherwise?

When I type Remove["Global`*"] foo[] := Module[{Subscript[a, 1] = "x"}, 0]; foo[] I get the expected error "only assignments to symbols are allowed". I ...
Nasser's user avatar
  • 150k
16 votes
1 answer
483 views

StringReplace, ReplaceAll and Rule interact in a bizarre way

I have the following pair of things: ClearAll[foo, labeledFoo]; labeledFoo = {"FooBarBazQuux", foo}; This works like you'd expect: ...
Pillsy's user avatar
  • 18.6k
15 votes
1 answer
880 views

Do Table iteration variables need to be localized using Module?

Lets say I define a Module, that internally use iterating in a Table or Do, say something ...
Per Alexandersson's user avatar
15 votes
2 answers
562 views

Why can't I inject expressions in Compile using (only) With

Generally speaking, With can be used to inject into arbitrarily held expressions, for example: ...
glS's user avatar
  • 7,753
15 votes
1 answer
357 views

Working with DynamicModule: SetDelayed + OwnValues

Shortly, definitions in form of e.g. x:= RandomReal[] have to be avoided inside DynamicModule[{x}, ...]. Why?, this ...
Kuba's user avatar
  • 138k
15 votes
2 answers
358 views

Is anonymous pure function a scoping construct?

In recent thread was raised the question: why anonymous pure functions Function[body] (or body &) do not rename symbols in ...
Alexey Popkov's user avatar
14 votes
4 answers
535 views

Using With to scope over pure functions

Consider this example: given an option for a function func as "x" :> (a&), how can one assign different values to ...
István Zachar's user avatar
14 votes
2 answers
3k views

Scoping in assigning a derivative

While answering another question, I stumbled upon a problem I cannot easily resolve. To assign the derivative of a function to another function, typically one can do this with a ...
freddieknets's user avatar
  • 1,085
14 votes
2 answers
552 views

Is Mathematica's lexical scoping broken?

Why do these two lines output different things (b and 0, respectively)? Is it a bug? ...
user541686's user avatar
  • 1,327
14 votes
1 answer
122 views

Bug when combining Dataset with a scoping construct and Map

The following fails in v11: ...
Mohammed AlQuraishi's user avatar
13 votes
4 answers
1k views

What is the accessibility of a package's `Private` context variables?

I've been reading up on how Mathematica handles contexts, $Context, $ContextPath, and a few of the tutorials they have on ...
sgdsgyhetwaraw's user avatar
13 votes
2 answers
458 views

RecurrenceTable iteration variable not localized? Bug or user error?

I recently lost a lot of time on this and need to understand whether I have misunderstood MMA operations or there's a problem. I am using MMA 10.1 on Windows 7 64-bit. The essence of the issue is ...
Julian Moore's user avatar
  • 2,570
13 votes
3 answers
513 views

Why does Module slow down StringMatchQ by 180 times?

I knew that Module is slower than With or Block, often by several times. But it surprises me ...
Lacia's user avatar
  • 2,789
13 votes
1 answer
978 views

Context of localised (dynamic) symbols

When you localise a symbol with Module, the created unique symbol is usually in the current context. There seems to be an exception to this which has further ...
halirutan's user avatar
  • 113k

1
2 3 4 5
12