当前位置: 首页 > 文档资料 > Clojure 中文教程 >

struct

优质
小牛编辑
123浏览
2023-12-01

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

语法 (Syntax)

以下是语法。

(struct structname values)

Parameters - 'structname'是要赋予结构的名称。 'values'是需要分配给结构的键值的值。

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

例子 (Example)

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

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

输出 (Output)

上述程序产生以下输出。

{:EmployeeName John, :Employeeid 1}

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