This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-05
Channels
- # aws-lambda (1)
- # beginners (294)
- # boot (35)
- # cider (19)
- # cljs-dev (39)
- # cljsrn (7)
- # clojars (48)
- # clojure (266)
- # clojure-android (1)
- # clojure-brasil (1)
- # clojure-france (2)
- # clojure-greece (5)
- # clojure-italy (7)
- # clojure-mexico (1)
- # clojure-russia (24)
- # clojure-spec (10)
- # clojure-uk (31)
- # clojurescript (134)
- # consulting (7)
- # cursive (69)
- # datomic (20)
- # emacs (57)
- # events (2)
- # figwheel (2)
- # hoplon (1)
- # jobs-discuss (19)
- # luminus (33)
- # lumo (18)
- # mount (1)
- # off-topic (32)
- # om (5)
- # onyx (27)
- # pedestal (15)
- # re-frame (12)
- # reagent (28)
- # rum (2)
- # schema (2)
- # spacemacs (9)
- # unrepl (2)
- # untangled (7)
- # vim (5)
- # yada (4)
I seem to remember that the fdef
:ret
is not enforced/checked under instrument
. Is that correct, and if so, how does one go about checking that the return value of a fn conforms to the spec?
Just to answer myself: https://groups.google.com/forum/#!topic/clojure-dev/4W4PO5Di9WU
The reason I was wondering (and wanting this) is that in cljs, I run my dev-setup with (stest/instrument)
which is super nice, but it would be nice to get the :ret
bit as well while running in dev-mode
@slipset The answer you found requires an additional binding in many cases, and will generally mess up the flow of the function. I'm in the same boat as you, looking for a way to automatically check the return, with the ability to disable it for production. Not having the ability to test :ret
except through stest makes a function spec much less useful. something like this works at least on a small example:
(defn foo [x y]
{:pre [(s/assert (:args (s/get-spec `foo)) [x y])]
:post [(s/assert (:ret (s/get-spec `foo)) %)]}
(+ x y))
it can be turned on/off with s/check-asserts
, so it may be a decent workaround to this limitation (it may be by design, but it is still a limitation)
as a https://github.com/Yuppiechef/datomic-schema user, who wrote a few DSLs in Rebol, I was inspired by the data model example in this article: http://blog.cognitect.com/blog/2017/4/6/developing-the-language-of-the-domain since there was no example implementation which could parse this:
(attr :university/full-name string non-blank unique
"Fully-expanded name of the university for public display")
I set out to implement it with clojure.spec
, so I can do:
(->> '[[:person/email one unique str "Email"]
[:person/org many ref "Orgs"]]
(mapv (partial s/conform ::attr))
clojure.pprint/pprint)
here is my implementation:
(defn with-ns [ns kw]
(keyword ns (name kw)))
(defn conform-with-ns [ns]
(s/conformer (partial with-ns ns)
(comp symbol name)))
(defn type-aliases [t]
('{str string} t t))
(s/def ::attr
(s/coll-of
(s/or :db/doc string?
:db/cardinality (s/and '#{one many}
(conform-with-ns "db.cardinality"))
:db/valueType (s/and '#{str string int ref}
(s/conformer type-aliases)
(conform-with-ns "db.valueType"))
:db/unique (s/and #{'unique}
(conform-with-ns "db.unique"))
:db/ident keyword?)
:into {}))