Hi. I am writing a small library (for learning purposes) and there are a
few macros. I have a macro that defines some object with the user-specified
name and internal some metadata:
(defmacro defobj [name operations] ; operations is a map
`(def ~name (with-meta ~operations {::my-obj true})))
Then users (and the library itself will do it) can call it to define
objects:
(defobj testobj {})
(meta testobj) ; gives {:user/my-obj true}
Now, I would like to create a macro that takes a name as an argument, and
does something with the object, but first I would like to check if the
object was created by 'defobj', i.e. check if the object's meta contains
the key ::my-obj. That's what I can do, I don't know how to get the meta of
the symbol:
(defmacro with-obj [name]
(let [obj-meta (meta name)]
(if (or (nil? obj-meta)) ; (not (::my-obj obj-meta)))
(throw (IllegalArgumentException. (str name " seems not be our
object")))))
`(println "nice"))
This always fails, as the meta is not fetched correctly; for example:
(macroexpand '(with-obj testobj))
IllegalArgumentException clojure.core$name@48183a9a seems not be our object
user/with-obj (NO_SOURCE_FILE:4)
So I have a few questions:
1. should I attach the meta to the object, or to its var? or maybe I
shouldn't do it at all, and just omit such checks altogether? (but I would
still like to know how to make it work ;d)
2. how do I retrieve the meta of an object that is specified by a symbol
passed to a macro by the user?
3. eventually, I would like my library to create a few default objects, so
they would be in my namespace (say, 'my.lib'), and the users should be able
to define their own objects as well in whatever namespace they wish; I
would love it if the meta checking in the macro worked with objects in all
namespaces, also if the symbols are namespaced or not
4. in the exception above, the symbol is resolved to the clojure.core/name,
which is wrong as well; I know I can change the symbol name, but I have the
feeling that this is not the way to go ;d
Could anybody help me with the macro in question and fetching the meta?
Regards,
wujek
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en