reader-conditional

added
1.7

ns
clojure.core

type
function

(reader-conditional form splicing?)

Construct a data representation of a reader conditional.
If true, splicing? indicates read-cond-splicing.

                ;;;; The data representation for reader conditionals is used by Clojure
;;;; when reading a string/file into Clojure data structures but choosing 
;;;; to preserve the conditionals themselves (instead of replacing them with
;;;; the appropriate platform-specific code)

;;;; Create a data representation for a NON-SPLICING reader conditional

(def r-cond (reader-conditional '(:clj (Math/random)) false))
;;=> #'user/r-cond
r-cond
;;=> #?(:clj (Math/random))
(:form r-cond)
;;=> (:clj (Math/random))
(:splicing? r-cond)
;;=> false

;;;; Data representation is same as the one produced by (read-string)
;;;; and (read) when provided with the option to preserve data conditionals

(= r-cond (read-string {:read-cond :preserve} "#?(:clj (Math/random))"))
;;=> true

;;;; Create a data representation for a SPLICING reader conditional

(def r-cond (reader-conditional '(:clj [Math/PI Math/E]) true))
;;=> #'user/r-cond
(:clj [Math/PI Math/E])
;;=> #?@(:clj [Math/PI Math/E])
(:form r-cond)
;;=> (:clj [Math/PI Math/E])
(:splicing? r-cond)
;;=> true

;;;; Data representation is same as the one produced by (read-string)
;;;; and (read) when provided with the option to preserve data conditionals

(= r-cond (read-string {:read-cond :preserve} "#?@(:clj [Math/PI Math/E])"))
;;=> true