> I needed (and created) a macro to generate keywords from symbols. I
> couldn't find anything ready, and I am still not completely
> comfortable with the solution I came up with. Any suggestions for
> improvement?
What are your exact requirements? Here's the most straightforward
solution:
(keyword 'abc)
:abc
(keyword 'x/y)
:x/y
But apparently you want to convert a non-qualified symbol to a
namespace-qualified keyword. Is that right?
Konrad.
Yes. The trick is that
(keyword 'x/y)
is different from
(ns x)
::y
Am 24.08.2009 um 18:31 schrieb Dragan Djuric:
> I needed (and created) a macro to generate keywords from symbols. I
> couldn't find anything ready, and I am still not completely
> comfortable with the solution I came up with. Any suggestions for
> improvement?
If I understand you correctly, you'd like to convert symbols which
name vars to keywords qualified with the namespace which contains the
var's definition. You don't really need a macro for that, a regular
function will do. Here's a version using var metadata:
(defn resolve-to-kw [sym]
(let [md (meta (resolve sym))]
(keyword (-> md :ns ns-name name) (-> md :name name))))
Hope this helps.
Kind Regards,
Achim
Am 24.08.2009 um 20:03 schrieb Laurent PETIT:
>> Yes. The trick is that
>> (keyword 'x/y)
>> is different from
>> (ns x)
>> ::y
>
> ??
::y is similar to `y. (Besides the resolving issue, which doesn't make
sense for keywords...)
For keyword there was some issue, that (keyword "foo/bar") creates a
keyword with "name" foo/bar and "namespace" "", while (ns foo) ::bar,
makes a keyword with namespace "foo" and name "bar".
Sincerely
Meikel
> Yes. The trick is that
> (keyword 'x/y)
> is different from
> (ns x)
> ::y
Not in my copy of Clojure:
user=> (ns x)
nil
x=> ::y
:x/y
x=> (identical? ::y (keyword 'x/y))
true
Konrad.