Public Vars

Back

vswap! (clj)

(source)

macro

(vswap! vol f & args)
Non-atomically swaps the value of the volatile as if: (apply f current-value-of-vol args). Returns the value that was swapped in.

Examples

frenchy64/fully-satisfies
(ns io.github.frenchy64.fully-satisfies.non-leaky-macros.clojure.core
  "Implementations of clojure.core macros that don't leak implementation details."
  (:refer-clojure :exclude [locking binding with-bindings sync with-local-vars
                            with-in-str dosync with-precision with-loading-context
                            with-redefs delay vswap! lazy-seq lazy-cat future
                            pvalues])
  (:require [clojure.core :as cc]))

(defmacro non-leaky-vswap!
  "Like clojure.core/with-redefs, except vol is only expanded once and .reset never reflects."
  [vol f & args]
  `(let [v# ~vol]
     (cc/vswap! v# ~f ~@args)))

(defmacro vswap!
  [& args]
  `(non-leaky-vswap! ~@args))