Clojure棱柱/架构defrecord没有枚举运行时验证?

时间:2015-11-25 08:27:18

标签: clojure clojurescript plumatic-schema

使用棱镜/模式时,defrecord上的枚举验证不起作用,如下所示:

(s/defrecord Action [type :- (s/enum :a :b)])
#'user/strict-map->Action
user> (Action. "3")            ; this should fail
#user.Action{:type "3"}
user> (Action. 1)              ; this should fail
#user.Action{:type 1}
user> (Action. "abc")          ; this should fail
#user.Action{:type "abc"}

但是,当我将enum更改为long时,它会按预期工作:

(s/defrecord ThisWorks [type :- long])
#'user/strict-map->ThisWorks
user> (ThisWorks. 3)
#user.ThisWorks{:type 3}
user> (ThisWorks. "abc")
ClassCastException java.lang.String cannot be cast to java.lang.Number  user/eval11888 (form-init4803894880546699153.clj:1)

有人知道吗?非常感谢你。

1 个答案:

答案 0 :(得分:3)

因为您可以switch on and off validation during runtime实际检查您的记录,直到您将它们传递给函数:

(s/defrecord Action [type :- (s/enum :a :b)])
(s/defn process-action [x :- Action])
(process-action (Action. "3")) ;; => Exception

关于long神奇地工作。这只是special clojure behavior due to primitives:

  

字段可以有类型提示,可以是原始的

     
      
  • 请注意,目前是   非原始类型的类型提示不会用于约束   字段类型也不是构造函数arg,但将用于优化它   在类方法中使用

  •   
  • 约束字段类型和构造函数   arg已计划

  •   
(s/defrecord PrimitveRec [foo :- long])
(s/defrecord NonPrimitveRec [foo :- String])


(.? NonPrimitveRec :field #"foo" :type)
;=> (java.lang.Object)
(.? PrimitveRec :field #"foo" :type)
;=> (long)

.?来自Vinyasa

相关问题