TL;DR; how can I enforce consistency when instantiating records?
Longer version:
In my application I have defined several records with defrecord
, and instances of this record get created a a few (3 or 4) places in the code, always using the map->...
constructor. For example map->Dfa
and map->State
. A Dfa
represents a finite state machine and a State
represents a state within such a machine. Each Dfa
has a collection of states, and each State
has transitions which reference other states within the same Dfa
.
Part of my development and debug time has been making sure that all transforms and combination functions which produce new Dfa
object only have valid transitions, ie. only transitions to states in the same Dfa
.
This is not really a constraint on the transformation functions, but is a constraint on the Dfa
record.
What is the best way to enforce the consistency of an object (record) so that it is impossible to construct an inconsistent record?
One way would be to write a factory function my-map->Dfa
, where I assert consistency, and the make sure no function calls map->Dfa
directly, but always goes through my-map->Dfa
.
Is that the correct way, or is there some way to augment construction of records in Clojure?