5
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ 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] ]; $\endgroup$
    – rm -rf
    Commented Feb 24, 2013 at 4:09
  • $\begingroup$ or maybe even DynamicModule[{x}, {x, Module[{x}, x]}] to see that the variables are different. $\endgroup$ Commented Feb 24, 2013 at 4:45
  • $\begingroup$ what i meant in the previous comment was evaluate the input DynamicModule[{x}, {x, Module[{x}, x]}] and observe the output. $\endgroup$ Commented Feb 24, 2013 at 6:20
  • $\begingroup$ Related: Syntax highlighter shows error for Manipulate when it's inside a Block or a Module. $\endgroup$ Commented Sep 15, 2014 at 14:21

2 Answers 2

8
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Commented Feb 24, 2013 at 3:30
  • 1
    $\begingroup$ @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. $\endgroup$
    – rm -rf
    Commented Feb 24, 2013 at 3:35
  • $\begingroup$ @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. $\endgroup$
    – Michael E2
    Commented Feb 24, 2013 at 3:36
  • $\begingroup$ @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 $\endgroup$
    – Rojo
    Commented Feb 24, 2013 at 4:06
  • $\begingroup$ @Nasser, then open Edit, Preferences, Appearance, Error and Warnings, and uncheck the warnings you don't like $\endgroup$
    – Rojo
    Commented Feb 24, 2013 at 5:11
1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ I understand very well what you mean, but in this case, I think that the explanation would be probably really to complicated. $\endgroup$
    – andre314
    Commented Feb 24, 2013 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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