On Mon, Sep 7, 2009 at 1:17 AM, Cliff Wells<cl...@twisty-industries.com> wrote:
>
> What I've tried (no laughing!) is this bit of code:
>
> (ns site.database
>  (:use [compojure])
>  (:use [clojure.contrib.sql])
>  (:use [site.setup])
>
>  (if (= *db-adapter* "mysql")
>    (:use [site.adapters.mysql :as adapter]))
>  (if (= *db-adapter* "postgresql")
>    (:use [site.adapters.postgresql :as adapter])))
>
> But this throws an error:
>
>   java.lang.Exception: No such var: clojure.core/if (database.clj:1)

The 'ns' there is a macro call, so the forms inside it don't
have to (and in this case very much do not) follow normal
evaluation rules.  That's why (:use ...) does something
other than looking up a keyword in a map, and also why (if
...) doesn't work.

Fortunately, the 'ns' macro expands into a bunch of regular
function calls.  You can find out exactly what any macro
expands to by using the 'macroexpand' function:

site.adapters.muser=> (macroexpand '(ns site.database
                                      (:use [compojure])
                                      (:use [site.adapters.mysql :as adapter])))

(do
  (clojure.core/in-ns (quote site.database))
  (clojure.core/with-loading-context (clojure.core/refer (quote clojure.core))
    (clojure.core/use (quote [compojure]))
    (clojure.core/use (quote [site.adapters.mysql :as adapter]))))

You can see each (:use ...) clause expands to a call to
a function named 'use'.  You can try one of these alone at
the REPL:

(use 'site.adapters.mysql)

So that should be all you need to know to call 'use'
selectively.  Outside the 'ns' macro call, after you're done
:use'ing the namespaces that won't change, you can use call
'use' or 'require' inside an 'if'.

> In any case, this code smells funny, even if it were to work.  It seems
> I should be able to somehow build a string such as (str "site.adapters."
> *db-adapter*) and use that instead (or something else equally concise).

'use' and 'require' except a symbol instead of a string, so
you could do something like:

(use (symbol (str "site.adapters." *db-adapter*)))

--Chouser

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to