Affects Version/s: None
Fix Version/s: None
Patch:Code and Test
Sometimes, when swapping or resetting an atom, it's desirable to know the value before the update. The existing swap! and reset! functions return the new value instead. Currently, the only option is to roll your own using a loop and compare-and-set!
Example use cases:
- When an atom contains a PersistentQueue and you want to atomically remove the head of the queue and process it: if you run (swap! q pop), you have lost the reference to the old head of the list so you can't process it.
- Want to check if an operation has occurred before by using atom as a flag (this can be achieved with compare-and-set! but reads a little easier this way).
- Want to use an atom similarly to a java.util.concurrent.LinkedTransferQueue, for the case of pairing up adds by writers and drainTo y readers:
- Extends clojure.lang.IAtom with derefReset and derefSwap with the exact same arities as their original variants, .reset and .swap. The deref-methods returns the old (deref-ed) value of the atom after a successful mutation of the atom.
- Added clojure.core/deref-reset! - just like reset! it unconditionally changes values of atoms but returns
the old atom value. Based on Steven Yi's get-and-set! in ticket
CLJ-1599, patch named get-and-set.diff
- Added clojure.core/deref-swap! - just like swap! but returns the old atom value.