Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

Mathematica graphics

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?

share|improve this question
2  
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 '13 at 4:09
    
or maybe even DynamicModule[{x}, {x, Module[{x}, x]}] to see that the variables are different. –  Mike Honeychurch Feb 24 '13 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 '13 at 6:20
add comment

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
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 '13 at 3:30
1  
@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 '13 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 '13 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 '13 at 4:06
    
@Nasser, then open Edit, Preferences, Appearance, Error and Warnings, and uncheck the warnings you don't like –  Rojo Feb 24 '13 at 5:11
show 2 more comments

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.

share|improve this answer
    
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 '13 at 9:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.