ClojureDocs

导航

命名空间

struct

clojure.core

自 1.0 起可用 (源代码)
  • (struct s & vals
Returns a new structmap instance with the keys of the
structure-basis. vals must be supplied for basis keys in order -
where values are not supplied they will default to nil.
2 Examples
user> (defstruct mystruct :foo :bar)
#'user/mystruct

user> (struct mystruct "eggplant" "pizza")
{:foo "eggplant", :bar "pizza"}
;; You can use an "anonymous" struct like so

(let [structure (create-struct :foo :bar)]
  (struct structure "pop" "fizz"))

;; This can be useful if you want to dynamically create/use structs, and don't
;; know the field names at compile time (e.g. when reading CSV files)
See Also

Same as (def name (create-struct keys...))

Added by dakrone

Returns a structure basis object.

Added by metasoarous
2 Notes
    By , created 10.2 years ago

    Structs are becoming obsolete. Use records instead. See defrecord.

    By , created 4.4 years ago, updated 4.4 years ago

    In general, records should be preferred over structs. However, structs aren't entirely obsolete.

    They can still be useful when you need/want to create record-like objects dynamically; That is, when you don't know the field names at compile time. A typical example of this might be loading rows from a CSV (as semantic-csv does). The advantage in this case over using regular maps is significantly improved performance creating and using these objects.