Since I am using it to create AgendaScope, i’ve written quite a lot of Elixir code to get myself up to speed. Most recenty a parser combinator library, Ergo. And it’s in building Ergo that I’ve realised I really miss a feature from Clojure, metadata.
In short Clojure allows you to attach arbitrary key-value metadata to any value. While there is syntax sugar it boils down to:
meta(obj)
to get the map of metadata key-value pairs for the value obj
.
vary-meta(obj, f, args)
to modify the meta-data associated with the value obj
.
If you make a copy of obj
the metadata is carried with it. In all other respects obj
is unaffected. Why is this so useful?
Well I first used it to manage whether a map, representing a SQL row, comes from the database or not as well as associated data about the record. I could have put that in the map using “special” keys but this pollutes the map with stuff not strictly related to its purpose and more complex code with special cases to handle those keys. But what if obj
wasn’t a map to which I could add special keys?
Clojure makes heavy use of it for self-documenting, tracking attributes, and tagging of all different kinds of structures.
What about Ergo? Well being a parser combinator library Ergo is about building functions, usually returning them as anonymous functions. When debugging a parser you’d really like to know more about it & how it was constructed but what you have is a function f
.
In Clojure I’d just put all that in metadata of f
and it would be no problem later to get at that metadata for debugging purposes by calling meta(f)
. The code of the parser itslef needing to know nothing about the metadata at all. But in Elixir… I am not sure what to do.
Elixir functions can have multiple heads so it occurs to me that I could return my parsers like:
fn
%{Context} = ctx -> do parsing stuff here
:info -> return some metadata instead
end
And this could work but also feels brittle to me. If a user defines a parser and doesn’t add an :info
head then my debugging code that invokes f.(:info)
is going to generate an error. It doesn’t smell right. Is this the best I can do?
I being to wish Elixir had an equivalent meta
functionality to Clojure.