Public Vars

Back

instance? (clj)

(source)

variable

(instance? c x)
Evaluates x and tests if it is an instance of the class c. Returns true or false

Examples

clojure
(deftest division
  (is (= clojure.core// /))
  (binding [*ns* *ns*]
    (eval '(do (ns foo
                 (:require [clojure.core :as bar])
                 (:use [clojure.test]))
               (is (= clojure.core// bar//))))))

(deftest Instants
  (testing "Instants are read as java.util.Date by default"
    (is (= java.util.Date (class #inst "2010-11-12T13:14:15.666"))))
  (let [s "#inst \"2010-11-12T13:14:15.666-06:00\""]
    (binding [*data-readers* {'inst read-instant-date}]
      (testing "read-instant-date produces java.util.Date"
        (is (= java.util.Date (class (read-string s)))))
      (testing "java.util.Date instants round-trips"
        (is (= (-> s read-string)
               (-> s read-string pr-str read-string))))
      (testing "java.util.Date instants round-trip throughout the year"
        (doseq [month (range 1 13) day (range 1 29) hour (range 1 23)]
          (let [s (format "#inst \"2010-%02d-%02dT%02d:14:15.666-06:00\"" month day hour)]
            (is (= (-> s read-string)
                   (-> s read-string pr-str read-string))))))
      (testing "java.util.Date handling DST in time zones"
        (let [dtz (TimeZone/getDefault)]
          (try
            ;; A timezone with DST in effect during 2010-11-12
            (TimeZone/setDefault (TimeZone/getTimeZone "Australia/Sydney"))
            (is (= (-> s read-string)
                   (-> s read-string pr-str read-string)))
            (finally (TimeZone/setDefault dtz)))))
      (testing "java.util.Date should always print in UTC"
        (let [d (read-string s)
              pstr (print-str d)
              len (.length pstr)]
          (is (= (subs pstr (- len 7)) "-00:00\"")))))
    (binding [*data-readers* {'inst read-instant-calendar}]
      (testing "read-instant-calendar produces java.util.Calendar"
        (is (instance? java.util.Calendar (read-string s))))
      (testing "java.util.Calendar round-trips"
        (is (= (-> s read-string)
               (-> s read-string pr-str read-string))))
      (testing "java.util.Calendar remembers timezone in literal"
        (is (= "#inst \"2010-11-12T13:14:15.666-06:00\""
               (-> s read-string pr-str)))
        (is (= (-> s read-string)
               (-> s read-string pr-str read-string))))
      (testing "java.util.Calendar preserves milliseconds"
        (is (= 666 (-> s read-string
                       (.get java.util.Calendar/MILLISECOND)))))))
  (let [s "#inst \"2010-11-12T13:14:15.123456789\""
        s2 "#inst \"2010-11-12T13:14:15.123\""
        s3 "#inst \"2010-11-12T13:14:15.123456789123\""]
    (binding [*data-readers* {'inst read-instant-timestamp}]
      (testing "read-instant-timestamp produces java.sql.Timestamp"
        (is (= java.sql.Timestamp (class (read-string s)))))
      (testing "java.sql.Timestamp preserves nanoseconds"
        (is (= 123456789 (-> s read-string .getNanos)))
        (is (= 123456789 (-> s read-string pr-str read-string .getNanos)))
        ;; truncate at nanos for s3
        (is (= 123456789 (-> s3 read-string pr-str read-string .getNanos))))
      (testing "java.sql.Timestamp should compare nanos"
        (is (= (read-string s) (read-string s3)))
        (is (not= (read-string s) (read-string s2)))))
    (binding [*data-readers* {'inst read-instant-date}]
      (testing "read-instant-date should truncate at milliseconds"
        (is (= (read-string s) (read-string s2) (read-string s3))))))
  (let [s "#inst \"2010-11-12T03:14:15.123+05:00\""
        s2 "#inst \"2010-11-11T22:14:15.123Z\""]
    (binding [*data-readers* {'inst read-instant-date}]
      (testing "read-instant-date should convert to UTC"
        (is (= (read-string s) (read-string s2)))))
    (binding [*data-readers* {'inst read-instant-timestamp}]
      (testing "read-instant-timestamp should convert to UTC"
        (is (= (read-string s) (read-string s2)))))
    (binding [*data-readers* {'inst read-instant-calendar}]
      (testing "read-instant-calendar should preserve timezone"
        (is (not= (read-string s) (read-string s2)))))))
clojure/core.typed
(ns ^:no-doc clojure.core.typed.import-macros
  (:require [clojure.core :as core]))

;copied from ClojureScript
(defmacro import-macros [ns [& vars]]
  (core/let [ns (find-ns ns)
             vars (map (core/fn [vsym]
                         {:pre [(symbol? vsym)]
                          :post [(instance? clojure.lang.Var %)]}
                         (let [v (ns-resolve ns vsym)]
                           (assert v (str "Internal error: " vsym " does not exist"))
                           v))
                       vars)
             syms (map (core/fn [^clojure.lang.Var v] 
                         {:pre [(instance? clojure.lang.Var v)]
                          :post [(symbol? %)]}
                         (core/-> v .sym (with-meta {:macro true})))
                       vars)
             defs (map (core/fn [sym var]
                         {:pre [(symbol? sym)
                                (instance? clojure.lang.Var var)]}
                         `(do (def ~sym (deref ~var))
                              ;for AOT compilation
                              (alter-meta! (var ~sym) 
                                           merge
                                           (dissoc (meta ~var) :ns :name)
                                           {:macro true})))
                       syms vars)]
    `(do ~@defs
         :imported)))
typedclojure/typedclojure
(ns ^:no-doc clojure.core.typed.import-macros
  (:require [clojure.core :as core]))

;copied from ClojureScript
(defmacro import-macros [ns [& vars]]
  (core/let [ns (find-ns ns)
             vars (map (core/fn [vsym]
                         {:pre [(symbol? vsym)]
                          :post [(instance? clojure.lang.Var %)]}
                         (let [v (ns-resolve ns vsym)]
                           (assert v (str "Internal error: " vsym " does not exist"))
                           v))
                       vars)
             syms (map (core/fn [^clojure.lang.Var v] 
                         {:pre [(instance? clojure.lang.Var v)]
                          :post [(symbol? %)]}
                         (core/-> v .sym (with-meta {:macro true})))
                       vars)
             defs (map (core/fn [sym var]
                         {:pre [(symbol? sym)
                                (instance? clojure.lang.Var var)]}
                         `(do (def ~sym (deref ~var))
                              ;for AOT compilation
                              (alter-meta! (var ~sym) 
                                           merge
                                           (dissoc (meta ~var) :ns :name)
                                           {:macro true})))
                       syms vars)]
    `(do ~@defs
         :imported)))
camsaul/methodical
(ns methodical.impl.combo.clojure
  "Simple method combination strategy that mimics the way vanilla Clojure multimethods combine methods; that is, to say,
  not at all. Like vanilla Clojure multimethods, this method combination only supports primary methods."
  (:require
   [clojure.core.protocols :as clojure.protocols]
   [methodical.interface]
   [methodical.util.describe :as describe]
   [pretty.core :as pretty])
  (:import
   (methodical.interface MethodCombination)))

  Object
  (equals [_ another]
    (instance? ClojureMethodCombination another))
camsaul/methodical
(ns methodical.impl.multifn.cached
  (:require
   [clojure.core.protocols :as clojure.protocols]
   [clojure.datafy :as datafy]
   [methodical.interface :as i]
   [methodical.util.describe :as describe]
   [pretty.core :as pretty])
  (:import
   (clojure.lang Named)
   (methodical.interface Cache MultiFnImpl)))

  Object
  (equals [_ another]
    (and (instance? CachedMultiFnImpl another)
         (= impl  (.impl ^CachedMultiFnImpl another))
         ;; TODO - does this make sense?
         (= (class cache) (class (.cache ^CachedMultiFnImpl another)))))

  Named
  (getName [_]
    (when (instance? Named impl)
      (name impl)))
  (getNamespace [_]
    (when (instance? Named impl)
      (namespace impl)))
camsaul/methodical
(ns methodical.impl.method-table.clojure
  (:require
   [clojure.core.protocols :as clojure.protocols]
   [methodical.impl.method-table.common :as method-table.common]
   [methodical.interface]
   [methodical.util.describe :as describe]
   [pretty.core :as pretty])
  (:import
   (methodical.interface MethodTable)))

  Object
  (equals [_ another]
    (and (instance? ClojureMethodTable another)
         (= m (.m ^ClojureMethodTable another))))