Datatypes

We have shown how to extend existing types to implement new abstractions with protocols, but what if we want to create a new type in Clojure? That is where datatypes come in.

A datatype provides the following:

  • A unique class, either named or anonymous

  • Structure, either explicitly as fields or implicitly as a closure

  • Fields that can have type hints and can be primitive

  • Optional implementations of abstract methods specified in protocols or interfaces

  • Immutability on by default

  • Unification with maps (via records)

We will use the deftype macro to define a new datatype, called CryptoVault, that will implement two protocols, including IOFactory.

Now that gulp and expectorate support several existing Java classes, let’s create ...

Get Programming Clojure, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.