tags:

views:

499

answers:

2

I am trying to get meta-data of all built-in Clojure functions.

In previous question I've learned that this can be achieved using something like ^#'func_name (get the var object's meta data). But I didn't manage to do it programmatically, where func-name is not known in advance.

For example trying to get the metadata of the last function in clojure.core:

user=> (use 'clojure.contrib.ns-utils)
nil
user=> (def last-func (last (vars clojure.core)))

user=> last-func
zipmap

;The real metadata (zipmap is hardcoded)
user=> ^#'zipmap
{:ns #<Namespace clojure.core>, :name zipmap, :file "clojure/core.clj", :line 1661, :arglists ([keys vals]), :doc "Returns a map .."}

;Try to get programmatically, but get shit
user=> ^#'last-func
{:ns #<Namespace user>, :name last-func, :file "NO_SOURCE_PATH", :line 282}

How can it be done? I tried numerous variations already, but nothing does the trick.

+6  A: 
kotarak
Thanks! Indeed, ^(ns-resolve 'clojure.core last-func) achieves this
bugspy.net
I found another way to do this using the "intern" function: ^(intern 'clojure.core last-func)
bugspy.net
This is a dangerous solution:<pre><code>user=> (meta (intern 'clojure.core (with-meta 'count :meta :data}))){:ns #<Namespace clojure.core>, :name count, :meta :data}</code></pre>So be careful where your symbol comes from. I would still recommend ns-resolve over intern.
kotarak
+3  A: 

Technically functions cannot have metadata in Clojure currently:

http://www.assembla.com/spaces/clojure/tickets/94-GC--Issue-90---%09-Support-metadata-on-fns

However, vars which are bound to functions may, and it looks like that's what you're finding with ns-resolve. (meta last-func) would work too. Since last-func is the var itself, ^#'last-func (which is shorthand for (meta (var (quote last-func)))) has a redundant var dereferencing.

technomancy
No. (meta last-func) doens't work. Try it yourself and see. It returns nil
bugspy.net
`clojure.contrib.ns-utils/vars` returns a list of symbols rather than a list of vars, that's why it doesn't work.
Brian Carper