5

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?

István Zachar
  • 47,032
  • 20
  • 143
  • 291
Nasser
  • 143,286
  • 11
  • 154
  • 359

2 Answers2

8

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.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • 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 03: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 03: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 03: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 04:06
  • @Nasser, then open Edit, Preferences, Appearance, Error and Warnings, and uncheck the warnings you don't like – Rojo Feb 24 '13 at 05:11
  • SetAttributes[f, HoldFirst];f[s_Symbol]:=Module[{s}, ... isn't necessarily an error. It's just another conflict that get's resolved in a particular way – Rojo Feb 24 '13 at 05:13
  • @Nasser You seem to be expecting a lot from the syntax highlighter... I can understand your gripe if the kernel leaked the variables between the scopes, but it doesn't. The FE uses what best knowledge it has of mma's syntax, the current scope, the variables defined (previously and now) and does a pretty good job. It is more likely that someone new might do this unknowingly and be confused that the x inside is not what they defined first, so mma gives a warning. Note that it is only a warning, not an actual error, so I don't see what the problem is. As Rojo said, disable it if you don't like it – rm -rf Feb 24 '13 at 06:00
1

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.

andre314
  • 18,474
  • 1
  • 36
  • 69
  • I understand very well what you mean, but in this case, I think that the explanation would be probably really to complicated. – andre314 Feb 24 '13 at 09:27