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 need a behaviour provided by:

x = 2;
Block[{x = HoldForm[x]}, x]
x

FrontEnd marks this syntax as not quite correct.

enter image description here

It has not caused me any troubles so far so my question is: could it be a problem?

share|improve this question
2  
I don't think it's a problem; the front end produces this coloring as a hint to the user that there could be a scoping inconsistency, not to say that the code is necessarily erroneous. The greater question in my mind is why does the FE apply the coloring in this particular case, which to me looks rather benign. (For others' reference, the FE is saying that x will go out of scope before being used. I must admit I don't understand what this even means for HoldForm.) –  Oleksandr R. May 23 at 23:55
    
The notebook code editor is far from perfect. It produces strange coloration in many other situations. –  m_goldberg May 24 at 2:26

1 Answer 1

up vote 7 down vote accepted

The FE only looks at the structure of your code for colouring. It doesn't evaluate anything. This means two things: (1) it can only guess that there might be a problem, because by looking at the structure, it doesn't know whether your code really evaluates to something you might not want. (2) You can easily trick the FE by changing the structure into something equivalent which looks different. With this you can easily trick the FE into whatever colouring you prefer. Here are only some examples of exactly the same code:

Mathematica graphics

Mathematica graphics

Mathematica graphics

Mathematica graphics

Mathematica graphics

share|improve this answer
1  
Or: Block[{x=HoldForm@x},x] –  Rolf Mertig May 24 at 6:27
    
@RolfMertig Included! –  halirutan May 24 at 8:28
    
@halirutan So do you think it is only artifact/preventive behaviour and not an idication of any subtle cases that I may face one day? –  Kuba May 24 at 8:36
2  
There is one issue: It seems you cannot remove the HoldForm wrapper inside the Block. If you use x=HoldForm[a] instead, everything works as expected. Look at the output of this and then replace a by x. –  halirutan May 25 at 1:28
    
I stumbled over this when I was trying to mess with the scoping by releasing the hold of x. I wanted to see what happens because the x inside the HoldForm is the global x. Obviously they prevented it. What consequences this has for your code, I don't know. –  halirutan May 25 at 1:30

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.