此函数用于定义由defstruct操作创建的类型的结构对象 。
(struct structname values)
参数 - " structname"是要赋予结构的名称, "values"是需要分配给结构键值的值。
返回值 - 返回一个结构对象 ,其值映射到结构的键。
(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对象的键。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)