clojure

How do I check to see if something is an Atom in Clojure?


I'm looking for the pedantic way to test whether something is an Atom or not, such as an (atom? ...) predicate in Clojure, similar to the family of (number? ...), (string? ...), (vector? ...), etc.

Given Atoms are a main language feature of Clojure, created with (atom ...), it feels wrong that I'd have to write my own custom function to test for an internal implementation class. e.g.,

(defn atom? [a] (= (type a) clojure.lang.Atom))

Is there more correct paradigm or built-in language feature I'm missing?


Note: this question is unrelated and not the same as Scheme's "atoms", (atom? ...), which are non-null cos-pairs.


Solution

  • I would use (instance? clojure.lang.Atom a) and not even bother writing a predicate.

    I would also caution that if you feel the need to write code that is conditional on the type of an object at runtime, you might want to take a step back and think of a different way to solve the problem: perhaps protocols or multi-methods would let you dispatch on the object type in a more idiomatic manner?