Fork me on GitHub
#clojure-spec
<
2020-04-23
>
fricze17:04:55

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

kenny17:04:42

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

kenny17:04:01

You can also

(s/def ::example-args (s/cat :x int?))

(s/fdef example
  :args ::example-args
  :ret int?)

fricze17:04:42

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

fricze17:04:58

like… most things in Clojure are easily movable and spec is not always like that. I’m curious why?

fricze17:04:31

it seems to work a bit opposed to rest of Clojure design

nikolavojicic19:04:59

I think in spec2 it will be (or it is already?) possible to transform spec -> map and vice versa.

pbrown23:04:57

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)