This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-29
Channels
- # admin-announcements (2)
- # beginners (10)
- # boot (253)
- # cider (11)
- # cljs-dev (26)
- # cljsjs (21)
- # cljsrn (7)
- # clojure (87)
- # clojure-berlin (13)
- # clojure-dusseldorf (5)
- # clojure-greece (7)
- # clojure-poland (11)
- # clojure-russia (189)
- # clojure-spec (31)
- # clojure-uk (86)
- # clojurescript (89)
- # cursive (15)
- # datavis (2)
- # datomic (57)
- # devcards (3)
- # dirac (92)
- # editors-rus (3)
- # emacs (4)
- # events (1)
- # funcool (30)
- # hoplon (3)
- # jobs-rus (6)
- # leiningen (1)
- # luminus (12)
- # mount (25)
- # off-topic (5)
- # om (43)
- # onyx (41)
- # perun (1)
- # proton (2)
- # protorepl (7)
- # re-frame (17)
- # reagent (34)
- # ring (13)
- # specter (1)
- # spirituality-ethics (1)
how can I spec the return spec of a function that depends on the args it received : ex ring-spec normal handler vs async-handler: (if args.length == 3 : ret then is nil else response map)?
a "multiple arity" version of fspec could make sense here. it would feel more natural maybe
You could make the ret spec nilable and describe that relationship in the :fn as you mentioned. Doesn't seem like that much of a hack to me
I think I prefer the s/or with the 2 fspecs, makes the relation between args/ret clearer
I came up with this in the end https://github.com/mpenet/ring-spec/blob/master/src/clj/qbits/ring_spec.clj#L81-L111
@mpenet: the whole purpose of :fn is to describe relationships between args and ret
got it, so not really good to discern 2 arities of a function like I was doing (I had one "giant" fspec describing both arities/return types)
seems perfectly reasonable to cover each possibility in the :ret (just tell the truth about what it can be)
then in :fn you can declare that the :ret you got matched the :args that were sent
you get conformed values of :args and :ret in :fn, so it should be relatively easy to check
Yes I saw this, but wouldn't it be more work for gen down the road having all these branches? I mean vs. (s/or (s/fspec) (s/fspec))
it just picks one or the other
that’s why I have a computer :)
a fully-featured dependent type system
@gfredericks: you joke, but: https://github.com/arohner/spectrum
@arohner: oh hey good!
basic tests pass, and simple files will check / not-check as appropriate, but nowhere near ready for production code
@mpenet I have some of the nastier core macro specs ready and some of them might land in the near future
gfredericks: Preferred schpec namespace for maps that are closed for extension and its generators? keys?
(weighted-or:
(defmacro weighted-or
"Like [[s/or]], but with weights."
[& weighted-options]
(let [[names specs ws] (apply map vector (partition 3 weighted-options))
sums (reductions + ws)]
`(s/with-gen
(s/or ~@(interleave names specs))
(fn []
(gen/fmap
(fn [count#]
(let [schema# (condp >= count# ~@(interleave sums specs))]
(gen/generate (s/gen schema#))))
(gen/choose 1 ~(last sums)))))))
)