clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
fricze 2020-04-23T17:07:55.269100Z

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

kenny 2020-04-23T17:15:42.269200Z

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

kenny 2020-04-23T17:17:01.269400Z

You can also

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

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

fricze 2020-04-23T17:53:42.270900Z

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

kenny 2020-04-23T17:54:51.271300Z

No

fricze 2020-04-23T17:54:58.271500Z

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

fricze 2020-04-23T17:55:31.271700Z

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

nikolavojicic 2020-04-23T19:42:59.271900Z

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

pbrown 2020-04-23T23:49:57.272100Z

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)