Sven Richter <sver...@googlemail.com> writes:

Hi Sven,

> What you are saying makes sense. I am always a bit shy to using macros
> as I have not understood them completely yet and everytime I tried I
> got distracted fast enough to not get it.

Syntax-quoting has not much to do with macros except that the latter
frequently use the former.

> So I just played a bit with it and I am failing already on the
> generation of the namespace.
>
> I have this function:
>
> (defn generate-ns [ns]
> (let [ns `(ns ~ns (:require [foo.bar :as bar]))]
> (clojure.pprint/pprint ns)))
>
> And calling it like this: (generate-ns "foons") will return this:
> (clojure.core/ns
> "foons"
> (:require [foo.bar :as leiningen.code-generator/bar]))
>
> However, what I need is this:
> (ns ;only ns here
> foons ; a symbol
> (:require [foo.bar :as bar])) ; only bar

Using qualified names (like clojure.core/ns) is perfectly fine except if
a human is going to read the code.  But you can force unqualified
symbols by using ~'sym.  And you can create a symbol from a string with
the function `symbol`.

So this function does what you want:

--8<---------------cut here---------------start------------->8---
(defn generate-ns [ns]
  (let [ns `(~'ns ~(symbol ns) (:require [foo.bar :as ~'bar]))]
    (clojure.pprint/pprint ns)))

(generate-ns "foons")
;(ns foons (:require [foo.bar :as bar]))
;=> nil
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to