I have some code that looks like this:

(ns foo.bar.endpoint.example
  (:require [compojure.core :refer [routes GET]]
            [duct.util.resource :as resource]))

(defn endpoint [config]
  (routes
   (GET "/" []
     (resource/url "foo/bar/endpoint/example/index.html"))))

In this case the resource/url function acts the same as
clojure.java.io/resource.

The problem is that I'd like to remove the repetition, so I extended
resource/url with the ability to derive the resource path from a namespace
object.

(defn endpoint [config]
  (routes
   (GET "/" [] (resource/url *ns* "index.html"))))

The problem with this approach is that the *ns* binding changes, and I want
to fix it at compile time. I could write:

(def this-ns *ns*)

(defn endpoint [config]
  (routes
   (GET "/" [] (resource/url this-ns "index.html"))))

But it's very tempting to use the eval reader form:

(defn endpoint [config]
  (routes
   (GET "/" [] #=(resource/url *ns* "index.html"))))

Or a macro:

(defn endpoint [config]
  (routes
   (GET "/" [] (resource/url (this-ns) "index.html"))))

Alternatively, I could do something with a quoted symbol:

(defn endpoint [config]
  (routes
   (GET "/" [] (resource/url `index ".html"))))

Or a keyword:

(defn endpoint [config]
  (routes
   (GET "/" [] (resource/url ::index ".html"))))

What does everything think is the best solution?

I must admit I'm tempted to use the eval reader, as it makes it very
explicit as to what's happening, while at the same time being very concise.

On the other hand, the eval reader isn't often used, and isn't actually
documented in the reader docs, just in the *read-eval* docs.

- James

-- 
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
--- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to