ClojureDocs

导航

命名空间

type

clojure.core

自 1.0 开始提供 (源代码)
  • (type x)
Returns the :type metadata of x, or its Class if none
2 Examples
;; Checking numbers
user=> (type 10)
java.lang.Long

user=> (type 10.0)
java.lang.Double

user=> (type nil)
nil

;; 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 string")
java.lang.String

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 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
See Also

Returns the Class of x

Added by gstamp

Returns the immediate and indirect superclasses and interfaces of c, if any

Added by kumarshantanu

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

Added by kumarshantanu
0 Notes
No notes for type