Fork me on GitHub
#clojure-spec
<
2018-07-25
>
jumar12:07:28

Let's say I have a strange function that has two arities: 1-arity computes some results and returns them 2-arity uses 1-arity to compute the same results and save the output to file. How can I write :ret spec for this? I tried :fn but struggling with reusing an existing spec

:fn (s/or :results (s/and #(= 1 (-> % :args count))
                                  ::result-spec)
                  :ouput-in-file (s/and #(= 2 (-> % :args count))
                                        #(-> % :ret empty?))))

Here, the ::result-spec isn't properly used - I need to apply it only to the :ret key value, not the whole map passed to :fn.

gfredericks12:07:18

if it were me I'd have two different functions

jumar12:07:18

That was my idea too, but let's say it's a "convention" to do it this way. Can I still somehow reuse the ::result-spec or is there a better way to write :ret spec for this function?

hiredman20:07:03

(s/def ::ret ::result-spec) (s/and ... (s/keys :req-un [::ret]))

jumar08:07:24

Nice, thanks! I guess the only problem would be if I had more than one such function in the same ns (conflicting ::ret specs) but that's not an issue in my case.