dissoc

How to generate specs for your Clojure project

This post describes how to generate specs for any Leiningen project. After 8 months of internships, teaching, and quals, I needed a refresher. You might also find it helpful.


First, add a dependency to lein-typed in your ~/.lein/profiles. Here’s what mine looks like.

~/.lein/profiles.clj
{:user {:plugins [[lein-typed "0.4.2"]]}}

Let’s generate specs for clj-time.

Shell
$ git clone git@github.com:clj-time/clj-time.git
$ cd clj-time

Next, make clj-time depend on core.typed.

clj-time/project.clj
:dependencies [...
               [org.clojure/core.typed "0.5.0"]
               ...]

Now, we generate specs for the clj-time.core namespace.

Shell
$ lein typed infer-spec clj-time.core

Our annotations have been inserted automatically in src/clj_time/core.clj. To end, here’s one of the generated annotations.

Result
(s/fdef
  days
  :args
  (s/alt :0-args (s/cat) :1-arg (s/cat :n int?))
  :ret
  (s/or
    :org.joda.time.Days
    (partial instance? org.joda.time.Days)
    :org.joda.time.PeriodType
    (partial instance? org.joda.time.PeriodType)))

12 Apr 2018