On Jan 17, 2009, at 4:11 PM, Dan Larkin wrote:
(defn require-resolve
  [id]
  (let [sym (symbol id)
        ns-symbol (symbol (namespace sym))
        var-symbol (symbol (name sym))]
    (require ns-symbol)
    (ns-resolve (find-ns ns-symbol)
                var-symbol)))

The name is terrible, I know.
Hi Dan,

That's interesting. I've given it some thought and I've come to see it as a version of resolve that tries harder than the default. Here's an implementation that makes its capabilities purely a superset of those of resolve. This version works well even with definitions that don't come from libs like those in clojure.core:
(defn resolve*
  [sym]
  (let [ns (namespace sym)
        name (name sym)
        ns (if ns (symbol ns) (ns-name *ns*))
        name (symbol name)]
    (or (and (find-ns ns) (ns-resolve ns name))
        (do
          (require ns)
          (ns-resolve ns name)))))

This version takes a symbol just like resolve does. It doesn't support passing in a string. I think any string to symbol transformation is easy one for a caller to make if it needs to. I had an interim version that supported separating the namespace and name into two arguments, but that would be more like ns-resolve* and resolve* is every bit as capable.
I'm interested in hearing any thoughts you may have on this version as  
it relates to the problem you're trying to solve. I like that in  
Clojure we now have a canonical way to load in a namespace definition:  
require. This idea of yours leverages that. Together with that, we  
also have a canonical way for one namespace to declare and satisfy its  
dependence on another: (ns (:require...)). Given those two facilities,  
I wonder what use case you envision for require-resolve (or resolve*).  
Is this intended to be used at the repl? Does it offer a compelling  
advantage for some kind of work over calling require directly and then  
letting the default symbol resolution mechanism do the work?
Stephen, if you think this is something suitable for clojure.contrib.ns-utils then I'll submit my CA to Rich and you can pull it in, otherwise it's here for anyone to use.
I think it's interesting experimental code. Please do send in a CA and  
let's get something like this into ns-utils.
--Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to