比较和设置!无条件重置clojure中的原子?
考虑clojure repl中的以下代码序列比较和设置!无条件重置clojure中的原子?,clojure,Clojure,考虑clojure repl中的以下代码序列 (def elems (atom {})) (swap! elems assoc 42 [:a 7]) elems 生成预期的{42[:a7]}。现在试试 (compare-and-set! elems elems (atom {})) 产生false,意味着比较和设置操作未成功。我很惊讶,因为我期望elems在比较和设置中测试与elems相同的内容操作。我知道我可以使用reset以实现无条件重置原子的目标,但我想知道为什么比较并设置的功能不完全
(def elems (atom {}))
(swap! elems assoc 42 [:a 7])
elems
生成预期的{42[:a7]}
。现在试试
(compare-and-set! elems elems (atom {}))
产生
false
,意味着比较和设置代码>操作未成功。我很惊讶,因为我期望elems
在比较和设置中测试与elems
相同的内容代码>操作。我知道我可以使用reset代码>以实现无条件重置原子的目标,但我想知道为什么比较并设置代码>的功能不完全相同?比较并设置代码>作用于原子引用的值,而不是原子本身
clojure.core/compare-and-set!
([atom oldval newval])
Atomically sets the value of atom to newval if and only if the
current value of the atom is identical to oldval. Returns true if
set happened, else false
你可能想要这个:
(compare-and-set! elems @elems {})
比较并设置代码>作用于原子引用的值,而不是原子本身
clojure.core/compare-and-set!
([atom oldval newval])
Atomically sets the value of atom to newval if and only if the
current value of the atom is identical to oldval. Returns true if
set happened, else false
你可能想要这个:
(compare-and-set! elems @elems {})