Moving contexts with $NewSymbol in Mathematica

I was playing around with $NewSymbol trying to find something to answer this question with. The docs say that

$NewSymbol is applied before the symbol is actually created. If the action of $NewSymbol causes the symbol to be created, perhaps in a different context, then the symbol as created will be the one used.

So I tried to automatically move a particular new symbol into a test context which should prevent its creation in the Global`* context, but the symbol gets created in both contexts.

In[1]:= Remove["Global`*"]
In[2]:= $NewSymbol=(Print[#1," : ",#2];
                    If[#1==="aLongTestSymbolName"&==="Global`",
                       Symbol["TestContext`"<>#1]])&;

In[3]:= x
During evaluation of In[3]:= x : Global`
Out[3]= x

In[4]:= aLongTestSymbolName
During evaluation of In[4]:= aLongTestSymbolName : Global`
During evaluation of In[4]:= aLongTestSymbolName : TestContext`
Out[4]= aLongTestSymbolName

In[5]:= Names["Global`*"]
Out[5]= {aLongTestSymbolName,x}

In[6]:= Names["TestContext`*"]
Out[6]= {TestContext`aLongTestSymbolName}

I believe that "aLongTestSymbolName" should not be in the Global` context. Can anyone see what I've done wrong or if I've misinterpreted the documentation?


Note: Having the symbol created in both contexts is not an option for the automatic highlighting in the above linked to question. The idea is to reserve certain symbol names such as "x"~~___ for variables and "f"~~___ for functions and then use string patterns in $NewSymbol to move the symbols to the appropriate highlighted context.

7
задан Community 23 May 2017 в 12:12
поделиться