1.3.0 permalink Arrow_down_16x16
  • (type x)
Returns the :type metadata of x, or its Class if none

2 Examples top

  • ;; Checking numbers
    user=> (type 10)
    java.lang.Long
    
    user=> (type 10.0)
    java.lang.Double
    
    
    ;; Checking collections
    user=> (type [10 20])
    clojure.lang.PersistentVector
    
    user=> (type '(10 20))
    clojure.lang.PersistentList
    
    
    ;; Checking other, but somewhat intuitive, forms
    user=> (type :a)
    clojure.lang.Keyword
    
    user=> (type Thread)
    java.lang.Class
    
    
    ;; Checking a symbol
    user=> (type 'whatever)
    clojure.lang.Symbol
    
    ;; A surprise attack yields
    user=> (type clojure.lang.Symbol)
    ;; not such a surprising response
    java.lang.Class
    
    
    ;; Checking a function
    user=> (defn foo [] ("any string"))
    #'user/foo
    user=> (type foo)
    user$foo
    
    
    ;; Checking a macro
    user=> (type fn)
    user$fn
    
    user=> (type clojure.core/fn)
    java.lang.Exception: Can't take value of a macro: #'clojure.core/fn (NO_SOURCE_FILE:94)
    
    
  • ;This example demonstrates how to add type information to regular clojure maps
    (defn purchase-order [id date amount]
      ^{:type ::PurchaseOrder} ;metadata
       {:id id :date date :amount amount})
    
    (def my-order (purchase-order 10 (java.util.Date.) 100.0))
    
    (my-order)
    => {:id 10, :date #<Date Sun May 15 14:29:19 EDT 2011>, :amount 100.0}
    
    (type my-order)
    => PurchaseOrder
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3052 top

(defn type 
  "Returns the :type metadata of x, or its Class if none"
  {:added "1.0"
   :static true}
  [x]
  (or (get (meta x) :type) (class x)))
Vars in clojure.core/type:
Used in 0 other vars

Comments top

No comments for type. Log in to add a comment.