1

With a fresh kernel, run

Unprotect[Quantity]; 
Quantity[x_Around, "DimensionlessUnit"] := x;

gives

SetDelayed::write: Tag Quantity in x_Around is Protected.

But run it a second time, the warning disappears.

Why doesn't Unprotect work at the first time?

matheorem
  • 17,132
  • 8
  • 45
  • 115
  • Works on MMA version 13.3 – Daniel Huber Dec 30 '23 at 08:19
  • @DanielHuber Thank you for testing. But I just tested in 13.3. It has the same behaviour as 13.2 – matheorem Dec 30 '23 at 09:03
  • Strange, I do not get an error message. I have Windows 10. – Daniel Huber Dec 30 '23 at 10:44
  • It works without errors on the cloud as well. – MarcoB Dec 30 '23 at 12:06
  • @MarcoB I do not know what is going on. But it is indeed happens on my computer https://drive.google.com/file/d/1FU4sCAEBBNByCUg2Vu2IBJRWjxpq3mXL/view?usp=sharing – matheorem Dec 30 '23 at 14:06
  • The duplicate question isn't totally clear about the solution. You want: Quantity; Unprotect[Quantity]; ...; Protect[Quantity];. The first use of the symbol must be outside of the Unprotect invocation. – Kuba hasn't forgotten Monica Jan 11 '24 at 18:58
  • This problem only reproduces for me in initialization cells on a fresh kernel. So the "I do not get error message" comments are because of that. – Kuba hasn't forgotten Monica Jan 11 '24 at 18:59
  • I also wonder whether ^= instead of := is better at both expressing intent and maintaining evaluation speed. The latter IIRC adds a global rule, whereas the former adds an upvalue to Quantity and doesn't modify the global rule set. I'd thus suggest Quantity[x_Around,"DimensionlessUnit"]^:=x; UpValues[Around] vs. Quantity[x_Around,"DimensionlessUnit"]:=x; DownValues[Quantity]. – Kuba hasn't forgotten Monica Jan 11 '24 at 19:03

0 Answers0