当 var 位于向量或列表中时,如何获取 Clojure 元数据?

How do I get to the Clojure metadata when the var is in a vector or a list?

我继承了 一堆 代码,我正试图将这些代码从 Lisp 翻译成 Clojure。 显然,除了句法差异外,还存在“文化”差异。 不要让任何人厌烦原因,这就是我的问题。

(def ^{:Metadata "metaA"} A "a")        ;; Define symbol A with a value and metadata.
=> #'thic.core/A
(def ^{:Metadata "metaB"} B "b")        ;; Define symbol B with a value and metadata.
=> #'thic.core/B

A
=> "a"                                  ;; A has a value.

B
=> "b"                                  ;; B has a value.

(meta #'A)
=>
{:Metadata "metaA",                     ;; Var A has metadata.
 :line 1,
 :column 1,
 :file "C:\Users\Joe User\AppData\Local\Temp\form-init1388145843259148568.clj",
 :name A,
 :ns #object[clojure.lang.Namespace 0x41c58b7e "thic.core"]}

(meta #'B)
=>                                      ;; Var B has metadata.
{:Metadata "metaB",
 :line 1,
 :column 1,
 :file "C:\Users\Joe User\AppData\Local\Temp\form-init1388145843259148568.clj",
 :name B,
 :ns #object[clojure.lang.Namespace 0x41c58b7e "thic.core"]}

(def V ['A 'B])                         ;; Define a vector of A and B.
=> #'thic.core/V

V
=> [A B]                                ;; Vector V is [A B].

(first V)
=> A                                    ;; A is the first entry in V.

(meta (var A))                          ;; A still has its metadata.
=>
{:Metadata "metaA",
 :line 1,
 :column 1,
 :file "C:\Users\Joe User\AppData\Local\Temp\form-init1388145843259148568.clj",
 :name A,
 :ns #object[clojure.lang.Namespace 0x41c58b7e "thic.core"]}

;;当元数据在 V 中时,如何获取 A 中的元数据?

(meta (first V))                        ;; This way doesn't work.
=> nil

(meta (var (first V)))                  ;; And THIS way doesn't work either.
Syntax error (ClassCastException) compiling var at (C:\Users\Joe User\AppData\Local\Temp\form-init1388145843259148568.clj:1:7).
class clojure.lang.PersistentList cannot be cast to class clojure.lang.Symbol (clojure.lang.PersistentList and clojure.lang.Symbol are in unnamed module of loader 'app')

一旦 var 进入列表或向量,它的元数据是否“消失”了?

V中的向量包含两个symbols instead of two vars.

您应该定义 V 以包含变量:

(def V [#'A #'B])

或者根据符号查找var:

(meta (ns-resolve *ns* (first V)))