Fork me on GitHub
#clojure-spec
<
2016-07-29
>
mpenet09:07:31

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)?

mpenet09:07:55

I guess I can hack something with :fn, but it doesn't feel right

mpenet09:07:56

a "multiple arity" version of fspec could make sense here. it would feel more natural maybe

mpenet09:07:52

(s/fspec (:args ... :ret) (:args :ret))

mpenet09:07:27

or maybe I can just write 2 fspec and just wrap them with (s/or ..)

gfredericks12:07:45

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

mpenet12:07:59

I think I prefer the s/or with the 2 fspecs, makes the relation between args/ret clearer

alexmiller13:07:55

@mpenet: the whole purpose of :fn is to describe relationships between args and ret

mpenet13:07:21

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)

alexmiller13:07:38

seems perfectly reasonable to cover each possibility in the :ret (just tell the truth about what it can be)

alexmiller13:07:53

then in :fn you can declare that the :ret you got matched the :args that were sent

alexmiller13:07:13

you get conformed values of :args and :ret in :fn, so it should be relatively easy to check

mpenet13:07:01

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))

mpenet13:07:17

I guess it depends what kind of functions we re talking about

alexmiller13:07:19

it just picks one or the other

alexmiller13:07:27

that’s why I have a computer :)

mpenet15:07:35

any teaser of what's coming in the next alpha?

gfredericks15:07:13

a fully-featured dependent type system

gfredericks16:07:20

@arohner: oh hey good!

arohner16:07:04

It still needs a whole lot of polish, but I’m convinced its sound

mpenet16:07:06

wow, impressive. will kick tires for sure

arohner16:07:24

it’s still basically at developer preview stage

arohner16:07:49

basic tests pass, and simple files will check / not-check as appropriate, but nowhere near ready for production code

alexmiller22:07:14

@mpenet I have some of the nastier core macro specs ready and some of them might land in the near future

lvh23:07:19

gfredericks: Preferred schpec namespace for maps that are closed for extension and its generators? keys?

lvh23:07:22

Also, weighted-or

lvh23:07:02

(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 [email protected](interleave names specs))
       (fn []
         (gen/fmap
          (fn [count#]
            (let [schema# (condp >= count# [email protected](interleave sums specs))]
              (gen/generate (s/gen schema#))))
          (gen/choose 1 ~(last sums)))))))
)

lvh23:07:41

Is there a pred for large integers?

lvh23:07:49

Like biginteger or bigint