Subscripts - Why do I see the error "only assignments to symbols are allowed" when using a Module and not otherwise?
This is because only symbols can be localized by Module
. It is not about assignment, but localization.
Subscript[a, 1]
is not a symbol, but a compound expression, so:
Module[{Subscript[a, 1] = "x"}, 0] (* <-- not allowed *)
Module[{}, Subscript[a, 1] = "x"] (* <-- allowed but not localized *)
I agree that the error you got may be a bit confusing.
A somewhat ugly workaround is Module[{Subscript}, Subscript[a, 1] = "x"]
or you may try to use the Notation`
package to create symbol names with subscripts in them. A word of warning though: in some cases, Module
variables that have DownValue
s do not get destroyed when the Module
finishes evaluating. For more information, see the end of the Module section in this answer by Leonid Shifrin, and the comments on that answer.
Module
only works with Symbols.
Head[ Subscript[a, 1] ]
Subscript
Perhaps you can use Internal`InheritedBlock
or Internal`LocalizedBlock
.
Subscript[a, 1] = "x";
Subscript[a, 2] = "y";
foo := Subscript[a, 1]
Internal`InheritedBlock[{Subscript},
Subscript[a, 1] = "new";
DownValues[Subscript]
]
Subscript[a, 1]
{HoldPattern[a1] :> "new", HoldPattern[a2] :> "y"} "x"
Note the different syntax and starting value within each Block:
Internal`InheritedBlock[{Subscript},
DownValues[Subscript]
]
Internal`LocalizedBlock[{Subscript[a, 1]},
DownValues[Subscript]
]
{HoldPattern[a1] :> "x", HoldPattern[a2] :> "y"} {HoldPattern[a1] :> System`Private`$Localized, HoldPattern[a2] :> "y"}
Internal`InheritedBlock[{Subscript},
Subscript[a, 1] = "x";
DownValues[Subscript]
]
Beware that neither function actually works like Module
:
Internal`InheritedBlock[{Subscript},
Subscript[a, 1] = "new";
foo
]
Internal`LocalizedBlock[{Subscript[a, 1]},
Subscript[a, 1] = "new";
foo
]
"new" "new"
a := "old"
b := a
Module[{a = "new"}, b]
"old"
It's just a mattern of Module
's syntax.
You'll see that the message was generated from Module
and not from Set
(=)
Note
In fact, Set
doesn't even seem to be called internally when you run a regular Module
(even though Trace
suggests it does)
We disable Set but it still works
In[42]:= ClearAll[x, y];
Block[{Set},
Module[{x = 8},
y = 9;
{x, y}
]]
Out[43]= {8, y}