How do I check if a value is a promise?

Availability:

(def x (promise))

      

How can I check if type x is a promise?

thank

+3


source to share


2 answers


I would say that you cannot at all. Since both parties are created as reify

several interfaces: IDeref

, IBlockingDeref

, IPending

, IFn

. But you can check whether the value of one of them implements: clojure.lang.IPending

.

(defn promise? [v]
  (instance? clojure.lang.IPending p))

      



Or maybe even everything:

(defn promise? [v]
  (every? #(instance? % v) 
         [clojure.lang.IPending 
          clojure.lang.IFn 
          clojure.lang.IBlockingDeref 
          clojure.lang.IDeref]))

      

+5


source


I would go with



(defn promise? [p]
  (isa? (class p) clojure.lang.IPending))

      

+2


source







All Articles