让我们看一下Leiningen项目映射的真实示例:global-vars。
;; Sets the values of global vars within Clojure. This example
;; disables all pre- and post-conditions and emits warnings on
;; reflective calls. See the Clojure documentation for the list of
;; valid global variables to set (and their meaningful values).
:global-vars {*warn-on-reflection* true
*assert* false}它允许leiningen用户为其项目范围重新定义Clojures全局变量的默认值。
现在,如果这个映射的键由关键字组成,我们将首先使用clojure.spec/keys来指定哪些键可以是映射的一部分,然后分别定义这些键下所期望的值。但是,由于clojure.spec/keys默默地忽略了:req和:req-un中的非关键字,并为:opt和:opt-un抛出了一个异常(如alpha15),我们将不得不在某种程度上解决这个问题。
我们可以通过以下方法获得这些全局变量的类型
(for [[sym varr] (ns-publics 'clojure.core)
:when (re-matches #"\*.+\*" (name sym))]
[varr (type @varr)])
=>
[*print-namespace-maps* java.lang.Boolean]
[*source-path* java.lang.String]
[*command-line-args* clojure.lang.ArraySeq]
[*read-eval* java.lang.Boolean]
[*verbose-defrecords* java.lang.Boolean]
[*print-level* nil]
[*suppress-read* nil]
[*print-length* nil]
[*file* java.lang.String]
[*use-context-classloader* java.lang.Boolean]
[*err* java.io.PrintWriter]
[*default-data-reader-fn* nil]
[*allow-unresolved-vars* java.lang.Boolean]
[*print-meta* java.lang.Boolean]
[*compile-files* java.lang.Boolean]
[*math-context* nil]
[*data-readers* clojure.lang.PersistentArrayMap]
[*clojure-version* clojure.lang.PersistentArrayMap]
[*unchecked-math* java.lang.Boolean]
[*out* java.io.PrintWriter]
[*warn-on-reflection* nil]
[*compile-path* java.lang.String]
[*in* clojure.lang.LineNumberingPushbackReader]
[*ns* clojure.lang.Namespace]
[*assert* java.lang.Boolean]
[*print-readably* java.lang.Boolean]
[*flush-on-newline* java.lang.Boolean]
[*agent* nil]
[*fn-loader* nil]
[*compiler-options* nil]
[*print-dup* java.lang.Boolean]剩下的我们可以通过阅读文档来填补。但是我的问题是:我们如何编写一个规范,以确保如果映射包含键'*assert*,它将只包含布尔值。
发布于 2017-04-17 19:32:14
FYI,目前还没有计划支持s/keys中的非关键字键.
有几种方法可以做到这一点(一种方法是在验证之前或在领先的构造器中执行类似clojure.walk/keywordize-keys的操作,然后使用s/keys)。
另一种方法是将地图视为映射条目元组的集合(一些宏可以很大程度上清理这一点):
(defn warn-on-reflection? [s] #(= % '*warn-on-reflection*))
(s/def ::warn-on-reflection (s/tuple warn-on-reflection? boolean?))
(defn assert? [s] #(= % '*assert*))
(s/def ::assert (s/tuple assert? boolean?))
(s/def ::global-vars
(s/coll-of (s/or :wor ::warn-on-reflection, :assert ::assert) :kind map?))最后,s/multi-spec可能是有趣的尝试,而不是s/or上面-这将使这是一个开放的规范,可以添加到以后。在某种程度上,开放这个规范可能很有用(所以也可以接受(s/tuple any? any?) --因为将来可能会添加一些新的东西,或者没有在这里列出(例如,还有来自其他名称空间的动态vars )。
另外,要注意这些属性规范中的一些内容。例如,*unchecked-math*被视为逻辑上的真值,特别是接受逻辑为真的特殊值:warn-on-boxed,但也会触发额外的行为。
https://stackoverflow.com/questions/43453776
复制相似问题