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!
andreset-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.