Clojure - 结构体



此函数用于定义一种类型的结构体对象,该对象由 defstruct 操作创建。

语法

以下是语法。

(struct structname values)

参数 − ‘structname’ 是要赋予结构体的名称。‘values’ 是需要赋予结构体键值的值。

返回值 − 返回一个结构体对象,其值映射到结构体的键。

示例

以下程序显示了如何使用它。

(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (defstruct Employee :EmployeeName :Employeeid)
   (def emp (struct Employee "John" 1))
   (println emp))
(Example)

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

上述程序产生以下输出。

{:EmployeeName John, :Employeeid 1}

可以清楚地看到,struct 函数中提供的值已分配给 Employee 对象的键。

clojure_structmaps.htm
广告