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,Modulesubstitutes new symbols for each of the local variables that appear anywhere inexprexcept 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 thexwithin the Module is actually evaluating to its global value. – Szabolcs Jul 03 '16 at 10:21SetSystemOptions["StrictLexicalScoping" -> True]fixes that. – Szabolcs Jul 03 '16 at 10:56f[x$_] = x$notx$1231. Moreover, the question is why a particular implementation was chosen. – Kuba Jul 03 '16 at 11:04Blockhas own issues, here 20766Blockfixes it butexpr(and all symbols that are really temporary) shouldn't beBlock'svariables, yet mixingBlockandModulewill generate the problem again... – Kuba Jul 03 '16 at 11:17Modulechange everyxit sees tox$1232? inner scoping constructs can deal with this, innerModulewill further rename tox$1232$1233so why not? That seems like a clear rule and meanwhile we have to workaround renaming that was supposed to make life easier. Again, StrictLexicalScoping isn't the answer here,x$is not enough imo. – Kuba Jul 03 '16 at 11:18SetandRule. So if you want to extend your thought about that specific part, here is a good place ;) – Kuba Jul 08 '16 at 07:12Module[{y}, Defer[f[x_] = x y]]– xzczd Feb 06 '22 at 15:18