3 ms·
Spec can only say what is included in a map, it can't exclude. And the spec is late-bound by the caller. Different callers mandate different specs for the same
by dustingetz 7y ago
Spec can only say what is included in a map, it can't exclude. And the spec is late-bound by the caller. Different callers mandate different specs for the same entity. I see that you know this but I object to the wording "constrained".
- TeMPOraL 7y agoFair enough. It's entirely unlike constraining in statically-typed languages, but in practice it can still work as constraints if you verify the specs as pre/post conditions of your functions (for which Clojure has some nice syntactic goodies available by default). The way we've used it, it worked almost as if the maps were statically-typed, and it saved my bacon quite a few times.
- lsh 7y agoclojure.spec + orchestra for nicely integrated pre/post spec checking https://github.com/jeaye/orchestra https://github.com/jeaye/orchestra
- filoeleven 7y agoSpec alpha 2 supports closed maps: https://github.com/clojure/spec-alpha2/wiki/Differences-from-spec.alpha https://github.com/clojure/spec-alpha2/wiki/Differences-from...
- roenxi 7y agoSpec is supremely flexible, it isn't hard to enforce a map having a specific set of keys even without explicit support. (ns user (:use clojure.set) (:require [clojure.spec.alpha :as s])) (s/def ::map-closed #(-> % keys set (difference #{:a :b :c}) empty?)) (s/valid? ::map-closed {:a 1, :c 2}) => true (s/valid? ::map-closed {:a 1, :c 2, :e 3}) => false