Search code examples
clojurenamespacesmultimethod

Multimethod dispatch on namespaced map namespace


Is it possible to dispatch based on a namespaced map namespace i.e. #:<this-thing>{}? Without hacks like printing or inspecting key prefixes?

I consider the last one hacky because a key prefix can be overridden:

(:qux/bar #:qux{:bar :baz}); => :baz
(:foo/bar #:qux{:foo/bar :baz}); => :baz
(:qux/bar #:qux{:foo/bar :baz}); => nil

Solution

  • This is not possible as this is just the visual representation of the map produced by writer. You would have to do the checks by yourself if all the keys in your map share the same namespace. Also the last example won't be produced by the writer - it will emit namespaced map literal only when all the keys share the same namespace.