比较和设置!无条件重置clojure中的原子?

比较和设置!无条件重置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以实现无条件重置原子的目标,但我想知道为什么比较并设置的功能不完全

考虑clojure repl中的以下代码序列

(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 {})