Datatypes

We’ve 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’s 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
  • Immutability on by default
  • Unification with maps (via records)
  • Optional implementations of abstract methods specified in protocols or interfaces

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

Get Programming Clojure, 3rd 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.