Consider the following snippet of code.
Clear[f, g]
f[x_] := Function[{i}, x];
g[h_] := Module[{Z},
Z[i_] := Print[h];
Print[h];
Z[1];
];
When g[f[2]] is called, the first Print correctly returns Function[{i$},2], while the second Print returns an error with Function[{1},2] as the output. The error arises from a conflict between the two local variables i's in f and Z. In general, Mathematica automatically renames variables to avoid conflict, as described here.
Why is there a conflict in this case? Other than choosing an alternative variable name, what are the useful guidelines one should follow to avoid such conflicts?
Answer
Using
SetSystemOptions["StrictLexicalScoping" -> True]
will fix this.
Related (long reading!):
Comments
Post a Comment