Hacker News new | ask | show | jobs
by Smaug123 1650 days ago
I don't really know what more you wanted from `Module[{y}, 1+y] /. y -> 10`. You've explicitly asked for a new symbol that is unrelated to the global one, and then replaced any occurrences of the global one with 10. Of course there are no occurrences of the global one.

I claim any semantics which resulted in `11` would be extremely confusing, because it would imply the following also resulting in `11`, which is obvious nonsense:

    y = 10;
    Module[{y}, 1+y]
1 comments

> I claim any semantics which resulted in `11` would be extremely confusing, because it would imply the following also resulting in `11`, which is obvious nonsense:

> y = 10; > Module[{y}, 1+y]

Yes that is precisely the point: because of Mathematicas weird evaluation model, you cannot have ordinary lexical scope as in every other programming language, but the only sensible thing to do is to rename variables under the hood. It is then very easy to leak those renamed variables accidentally in the global scope where you cannot do anything useful with them, because the symbols `y` in the global scope and in the subscope are different.

Another point that the example was meant to illustrate is that once you assign some value to a variable, you can no longer do symbolic manipulation with it (note that in the example, we have replaced 1 by 10 in the end).