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 a ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.