I’m curious about specing functions. There’s probably something I don’t understand, but why function specs made with fdef
are not reusable? I can easily imagine, and even find in my codebase, few functions that share the same spec, but still fdef
have to be called with all the arguments for each function. If anyone has some insight into this decision, I’d love to hear something 🙂 thanks
Not sure if I'm following what you mean but you can do this:
(s/fdef example
:args (s/cat :x int?)
:ret int?)
=> user/example
(s/valid? (:args (s/get-spec `example)) (list 1))
=> true
You can also
(s/def ::example-args (s/cat :x int?))
(s/fdef example
:args ::example-args
:ret int?)
yeah, I can do that but can I do something like
(s/def ::example-fn-spec
{:arg (s/cat :x int?)
:ret int?})
(s/fdef example-fn ::example-fn-spec)
(s/fdef example-fn-2 ::example-fn-spec)
?No
like… most things in Clojure are easily movable and spec is not always like that. I’m curious why?
it seems to work a bit opposed to rest of Clojure design
I think in spec2 it will be (or it is already?) possible to transform spec -> map and vice versa.
In spec1 s/def
can refer to another spec by fully qualified symbol too, so this should work:
(s/fdef f1 ...)
(s/def f2 `f1)