I am redefining some of the standard math functions. I know this is dangerous, so I am trying to be extra careful. (For instance, in the redefinitions, I make sure to only match a custom head)
The redefinitions generally went smoothly. However, I noticed that the following code bit produces an error, but only when you run it the first time (using a "fresh" Kernel):
Unprotect[Plus];
Plus[p_customHead, q_Quantity] := p + q
Protect[Plus];
-> SetDelayed::write: "Tag Plus in p_customHead+q_Quantity is Protected. "
If you run the snippet a second time, it works, and the subsequent code behaves as expected (my actual redefinition is a bit more complicated and produces results that are different from a simple p+q
).
I already feel uneasy about redefining the standard math functions, and this behaviour makes me doubt if I should venture any further in this direction. Is this sort of thing to be expected when using Unprotect
?