# Why does the syntax highlighter color a local symbol red inside DynamicModule?

I am adding a Module inside DynamicModule here is an example (thanks to @rm-rf for this simple example)

DynamicModule[{x = 1}, Module[{x}, x = 2] ];


This Module has a local symbol x which happens also to be the name of another local symbol in the enclosing DynamicModule.

Yet, the front end is making all occurrences of x as red inside the Module. Here is a screenshot:

The question is: Why would local symbols to one Module conflict with local symbols of the enclosing DynamcModule? Yet, the code runs with no problem. So, could this just be that the FE is confused? Or am I doing something wrong?

-
Your example is needlessly complicated and makes it harder to convey the point... here's a simple one: DynamicModule[{x = 1}, Module[{x}, x = 2] ]; – rm -rf Feb 24 at 4:09
@rm-rf good point. I just copied one example I was having hard time with. Will update my question to use your example now. – Nasser Feb 24 at 4:36
or maybe even DynamicModule[{x}, {x, Module[{x}, x]}] to see that the variables are different. – Mike Honeychurch Feb 24 at 4:45
what i meant in the previous comment was evaluate the input DynamicModule[{x}, {x, Module[{x}, x]}] and observe the output. – Mike Honeychurch Feb 24 at 6:20

I think it's just a warning that you have a symbol inside a localization scope that contains a symbol of the same (base) name. Compare

With[{max = 2}, {With[{max = 3}, max], max}]

Module[{max}, max = 2; {Module[{max}, max = 3], max}]

Block[{max}, max = 2; {Module[{max}, max = 3], max}]


and so on. The outputs above are each {3,2} and no messages are generated, but the inside maxs are red. A bit annoying, perhaps.

If you look at the menu command Help > Why the Coloring... the palette indicates a local scope conflict. It's not an error, but a possible mistake. (It's a common mistake for beginners, probably.)

Here is an answer to a related question.

Update

Whether to color local scope conflicts and other warning, and what color to use, can be set with the menu command Preferences > Appearance > Errors and Warnings, which is mentioned in the answer mentioned above.

-
Thanks. I understand this. But the point is that it should not matter if a module is inside another. From help it says Module allows you to set up local variables with names that are local to the module. So, what is the point of having symbols local to a module, if they will clash with symbols of an enclosing Module? Also, the same case works with Manipulate, and Manipulate is a DynamicModule as well. I guess the question then: When is a red colored symbol to be then taken seriously as due to a real error or due to something else, which is not a real error? – Nasser Feb 24 at 3:20
I agree with Michael E2. It would appear that the Mathematica code editor's syntax analyzer isn't up handling these nested constructs. I suppose it might be considered a bug, but like Michael I take it as a warning. – m_goldberg Feb 24 at 3:30
@Nasser There are several examples of the highlighter showing something as red when it's not really an error. I'm also not surprised that it can't know well in advance that this won't lead to an error, so better safe than sorry. The only true answer to your question is to treat it as a warning and proceed if you know what you're doing. – rm -rf Feb 24 at 3:35
@Nasser You get the same red on the equal sign with If[x = y,...]`, which is not an error either. It's often just a mistake. – Michael E2 Feb 24 at 3:36
@Nasser, they won't clash but can you picture a situation where you used the same name in both nested modules by mistake and then tried to refer to the outermost variable with that common name? Just a warning – Rojo Feb 24 at 4:06

This is a intended feature.

It is explained by J. Fultz (Wolfram) in http://forums.wolfram.com/mathgroup/archive/2011/Sep/msg00198.html

At that time the last mathematica version was version 8 and the feature didn't exist.

-
 Yes, Ive seen that before. Forgot about it. But it would be nice to know WHY. I do not like just to remember rules about a language. There should be logical reason for these rules. Mathematica is full of just rules to remember, but very little logic to explain why things work the way they are, that is why I learn it by trial and error, because many times it is just impossible to reason about why it does what it does. – Nasser Feb 24 at 9:06 I understand very well what you mean, but in this case, I think that the explanation would be probably really to complicated. – andre Feb 24 at 9:27