InfoQ Homepage News Clojure 1.9 Brings Spec and More Language Features

Clojure 1.9 Brings Spec and More Language Features


As InfoQ previously reported, the most interesting new feature in the recently released Clojure 1.9 is Spec, which provides a standard and integrated system for the specification and testing of data and functions.

Leveraging previous work on other contract systems such as Racket’s, Spec aims to make it possible to automatically validate Clojure code, as well as to support a number of tasks such as generative testing, error reporting, destructuring and more. This is how you can specify a map, using spec/keys:

(spec/keys :req [::x ::y (or ::secret (and ::user ::pwd))]
           :opt [::z])

Keys used in a map specification are somewhat reminiscent of discriminated union tags as found in other languages, in that you define their types globally for a namespace:

(spec/def ::x integer?)
(spec/def ::y integer?)
(spec/def ::z integer?)
(spec/def ::secret string?)

This means that the same type specification is applied to occurrences of a given key in any map defined in the same module. If you want to use keys defined in a different namespace, you can use the new map namespace syntax:

#:types{ :x 1
         :y 2
         :secret "xyz" }

This is interpreted as

{ :types/x 1
  :types/y 2
  :types/secret "xyz }

While Clojure 1.9 is now stable, Spec is still in alpha state and the corresponding libraries must be included. Being alpha implies that API compatibility is not guaranteed.

To increase Spec expressiveness, Clojure 1.9 has added a number of new predicates, including boolean?, double?, simple-symbol, and many more. Still on the language front, clojure.core introduces the following new functions:

  • bounded-count, which allows to count the elements of a collection without realizing it beyond a bound.
  • swap-vals! and reset-vals!, two new atom functions that return both the old and new values.
  • halt-when, a transducer that ends transduction when a given predicate is satisfied.

Clojure 1.9 also introduces several performance enhancements, including optimized seq and destructuring, class derivation caching to speed up compilation, and others.

Clojure 1.9 may be installed using brew on macOS, and an install script on Linux. On Windows, you are still required to install Leiningen or Boot instead.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.