Specifying Clojure function schemas with Malli25 Dec 2023
Clojure is a dynamically typed language which means that functions can be called with arbitrary values. If the function cannot operate on the values (or if it calls a function which cannot operate on a value or part of a value) a runtime exception is thrown. Even worse, a function could exhibit undefined behaviour for the values provided.
Statically typed languages are often considered safer because the types of values are known at compile time so that the validity of values can be checked. However statically typed implementations tend to become inflexible. Type checking at compile time requires the types of the values held by variables to be defined at compile time. When trying to keep the system both modular and extensible, the developer is forced to come up with complicated type hierarchies and possibly even generic types.
There is a promising solution to this and this is using pre- and post-conditions (contracts) for functions. In particular there is Malli which allows you to specify schemas for each function using a concise syntax. Malli schemas can be defined, reused, and composed like types. Note that a value can fulfill multiple schemas (similar to Rust traits), however implementations and schemas exist independently from each other. This allows schemas to be more specific than static types will ever be (e.g. you can have a schema for lists with three or more elements).
Malli is optimized for good runtime performance. If performance is still a concern, one can leave schema validation disabled in production for some or all namespaces.
Here follows a short introduction to using Malli for function pre- and post-conditions:
You can use the Malli provider module to suggest possible schemas matching a list of values.
The resulting schemas can be used to validate values.
Schemas are values which can be reused and composed.
It is possible to add predicate functions to schemas.
Most importantly one can add pre- and post-conditions (function schemas) to functions.
By default function schemas are ignored until the schemas are collected and the functions are instrumented. Note that here I am replacing the default exception thrower with a more human-readable one.
If you want to collect the schemas from a module (e.g. for testing purposes), you can specify the namespace as follows.
You can also parse code to infer a general schema for a function header.
If you have a multiple arity function, you can use :function to specify the different alternatives.
There is also support for sequence schemas which allows a more compact schema in this case.
Finally here is an example for a method accepting keyword arguments which took me some time to figure out.
The only problem I encountered so far is that instrumentation clashes with Clojure type hints. I guess the solution is to instead provide the type hints at the call site instead where necessary for performance reasons.
You can also define recursive schemas by using a local registry:
One can use m/=> to add a Malli schema to a function. This comes in handy when you need to add a schema to a Clojure multimethod.