我有这个数据结构(基本上):
(setq ssm-list '(tasklist
((id . "10525295")
(name . "Inbox")
(sort_order . "0"))))
这适用于获取名称:
(defun ssm-list-get-prop (list prop)
(cdr (assoc prop (car (cdr list)))))
(ssm-list-get-prop slack-one-list 'name)
我喜欢创建一个宏,它将创建一个名为ssm-list-name
(或ssm-list-id
)的defun,因为列表中实际上有更多属性。
所以我尝试了这个:
(defmacro ssm-list-prop-defun (field)
`(defun ,(intern (concat "ssm-list-" field))
(one-list)
(cdr (assoc ,field (car (cdr one-list))))))
(ssm-list-prop-defun 'name)
(ssm-list-prop-defun 'id)
但最后两次调用失败了(wrong-type-argument characterp quote)
我尝试将symbol-name
放入宏中,但这没有帮助。
有没有办法实现这个目标?
答案 0 :(得分:4)
你非常接近,小编辑会让你找到一个有效的解决方案。问题是你要混合符号和字符串。这将有效:
(defmacro ssm-list-prop-defun (field)
;; note that concat operates on strings
`(defun ,(intern (concat "ssm-list-" field))
(one-list)
;; note that you need a symbol here, so quote the
;; result of the call to intern
;; and, if you're always using symbols,
;; might as well use assq
(cdr (assq ',(intern field) (car (cdr one-list))))))
;; pass in a string
(ssm-list-prop-defun "name")
这是使用符号的变体:
;; variant that works off a symbol
(defmacro ssm-list-prop-defun (field)
`(defun ,(intern (concat "ssm-list-" (symbol-name field)))
(one-list)
(cdr (assq ',field (car (cdr one-list))))))
(ssm-list-prop-defun name)
答案 1 :(得分:0)
我想您想了解cl包中的defstruct
:(info "(cl) Structures")
(或http://www.gnu.org/software/emacs/manual/html_node/cl/Structures.html#Structures)