I am working through the Programming Clojure book. While explaining alter and the STM, they say that if, during an alter, Clojure detects a change to the ref from outside the transaction, it will re-run the transaction with the new value. If that is the case, I would imagine the update function you pass in needs to be pure, but that isn’t indicated in the docs (and it is in other similar situations).
So is my assumption correct? If not, how does the STM re-apply the function? If it is correct, is it the case that you can’t rely on the docs to tell you when you can have side effects, and when you can’t?
It doesn’t strictly have to be pure, it just has to be idempotent. In practice this is basically the same thing.
Further, it only has to be idempotent when seen outside of the STM: if the only side effect you produce is writing to some other ref or (I think) sending to an agent, that operation will be held until your transaction has succeeded.
It’s also not really the case that it has to be any of these things: just that, if your update function isn’t pure, the results may not be what you expect.
Edit:
dosync‘s docs tell you that any expressions in the body may be executed more than once. You can’t run analterwithout running adosync, so it looks like all the docs you need are there. What would you like changed?