Fork me on GitHub
#clojure-spec
<
2019-12-14
>
Kevin11:12:48

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

taylor13:12:27

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

Kevin14:12:10

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

Alex Miller (Clojure team)13:12:10

Stay tuned for spec 2.... :)

Kevin14:12:28

Looking forward to that!

Alex Miller (Clojure team)16:12:55

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

👏 32
parrot 8
valerauko14:12:56

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

Alex Miller (Clojure team)14:12:16

I did a talk at Clojutre that talks about it some

valerauko06:12:31

in your Nice and Accurate Counsel?

Kevin16:12:41

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

Kevin16:12:25

it’s also difficult to convince my colleagues 😄