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
kwrooijen 2019-12-14T11:32:48.011100Z

Hey, I was wondering if there was a shorthand for creating fdefs , For example I have:

(s/fdef new-person
  :args (s/cat :name :person/name
               :age :person/age)
  :ret :app/person)
(defn new-person [name age]
  {:person/name name
   :person/age age})
Which works, but it’s quite a lot noise (seeing that the fdef is larger than the function itself I know I shouldn’t be comparing spec to type signatures, but that’s basically what I’m doing here. Something like this would be pretty useful for me:
(s/sig new-person [:person/name :person/age] :app/person)
(defn new-person [name age]
  {:person/name name
   :person/age age})

taylor 2019-12-14T13:27:27.011300Z

I think I’ve seen a few libraries that have macros combining defn and fn specs

taylor 2019-12-14T13:29:01.011500Z

here’s one https://github.com/danielcompton/defn-spec

alexmiller 2019-12-14T13:53:10.012600Z

Stay tuned for spec 2.... :)

kwrooijen 2019-12-14T14:19:10.012700Z

Thanks, these sort of do what I want. But they replace defn which I don’t like. I couldn’t find anything so I ended up writing a quick macro:

(defmacro defsig
  [fname fargs fret]
  `(clojure.spec.alpha/fdef ~fname
     :args (clojure.spec.alpha/cat ~@(apply concat (map vector (map (comp keyword str) (range)) fargs)))
     :ret ~fret))

kwrooijen 2019-12-14T14:21:28.013Z

Looking forward to that!

alexmiller 2019-12-14T16:02:55.014Z

Rich is working on the design for this right now and it’s starting to look pretty good

👏 8
🦜 2
valerauko 2019-12-15T14:14:56.016800Z

I'd be interested to hear what your workflow (feedback loop?) is for developing the language. Are there any blogs about that?

alexmiller 2019-12-15T14:48:16.018200Z

I did a talk at Clojutre that talks about it some

valerauko 2019-12-18T06:30:31.029300Z

in your Nice and Accurate Counsel?

kwrooijen 2019-12-14T16:23:41.015400Z

That would be such an amazing improvement for me. I’d honestly want to spec every function at my day job (I guess that’s the static typing mindset in my head talking), but it’s just too verbose at the moment

kwrooijen 2019-12-14T16:24:25.016200Z

it’s also difficult to convince my colleagues 😄