In a recent question [1] OP asked how to make Module
scope working inside Set
, so how to get:
f[x$123_] = x$123
instead of
f[x_] = x
from
Module[{x}, Defer[f[x_] = x] ]
It is stated in Details of Module
ref page that:
Before evaluating
expr
,Module
substitutes new symbols for each of the local variables that appear anywhere inexpr
except as local variables in scoping constructs.
My question is - why this exception is present? What common usage cases dictated this behaviour? Does this makes something safer?
I don't complain, I'm just curious. I don't know why Module can't just change every x
it sees to x$1232
? Current rule looks like an exception which causes only troubles.
It is even more interesting when we introduce y
which will rename x
Module[{x , y = 1},
Defer[f[x_] = x y]
]
f[x$_] = x$ y$7519
x = 1; Module[{x = 2}, Print[x_ -> x]]
Note how thex
within the Module is actually evaluating to its global value. $\endgroup$SetSystemOptions["StrictLexicalScoping" -> True]
fixes that. $\endgroup$f[x$_] = x$
notx$1231
. Moreover, the question is why a particular implementation was chosen. $\endgroup$Set
andRule
. So if you want to extend your thought about that specific part, here is a good place ;) $\endgroup$