TL;DR; how can I enforce consistency when instantiating records?
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
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
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
This is not really a constraint on the transformation functions, but is a constraint on the
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
Is that the correct way, or is there some way to augment construction of records in Clojure?