I want to add metadata to a byte array in Clojure. Since this is not allowed, one option I want to try is the simplest object wrapper that could work.
Here is the source code for with-meta.
That made me start looking at Clojure.lang.IObj
. I haven't found what I want yet.
Here's how you can create a
deftype
that supports metadata.And here's some example usage:
This is the simplest way I can think of to put metadata on a byte array (
reify
doesn't work withclojure.lang.IObj
and records include more unrelated functionality).Another option (and perhaps simpler depending on the context) would be to store the byte array in a map, with the metadata either right alongside it or as actual metadata.
After discussing with some people on #clojure IRC, I wrote a simple Java class,
MetaBox
, that implementsclojure.lang.IObj
. You can use this easily in Clojure withmetabox/box
andmetabox/val
as well as the usual metadata functions such asmeta
andwith-meta
.You can find the source code and README over at clj-metabox.
UPDATE: Thanks to a few discussions and suggestion (see below), as of version 0.2.0, the API uses
deref
instead ofval
: