无涯教程-Clojure - struct函数

117 阅读1分钟

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

struct - 语法

(struct structname values)

参数      -  " structname"是要赋予结构的名称, "values"是需要分配给结构键值的值。

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

struct - 示例

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

上面的程序产生以下输出。

{:EmployeeName Learnfk, :Employeeid 1}

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

参考链接

www.learnfk.com/clojure/clo…