def interns the var at compile time, binding is a runtime operation

> On 2 Dec 2015, at 16:56, Gregg Reynolds <d...@mobileink.com> wrote:
> 
> 
> 
> On Wed, Dec 2, 2015 at 8:11 AM, gianluca torta <giato...@gmail.com> wrote:
> for some reason, it looks like the implementation of the ns macro assumes 
> that the macro itself is being defined in namespace clojure.core
> 
> try defining myns in the namespace clojure.core, and it should work
> 
> Thanks, Gianluca.  I tried that but def macros don't seem to respect 
> rebinding *ns*.
> 
> (ns foo)
> (println *ns*) ;; => #object[clojure.lang.Namespace 0x73eb2c69 foo]
> (binding [*ns* (create-ns 'clojure.core)]
>   (println *ns*) ;; => #object[clojure.lang.Namespace 0x255f1d1 clojure.core]
>   (def bar "i should be in clojure.core!")
>   (println (var bar))) ;; => #'foo/bar
> 
> I also tried in-ns and with-redefs to no avail.
> 
> Interning seems to work:
> 
> (ns foo)
> (defmacro co-ns
>   ... code from clojure.core/defmacro ns...)
> (intern 'clojure.core (with-meta 'co-ns {:macro true}) @#'co-ns)
> 
> This puts the co-ns macro in clojure.core, but I still have to qualify the 
> call: foo/co-ns or clojure.core/co-ns.  I haven't been able to get (co-ns 
> ...) to work at top of file, but maybe it's better to qualify it anyway.
> 
> In the end it's embarrasingly simple:
> 
> ;; foo.clj
> (ns foo)
> (defmacro co-ns
>   [name & references]
>   ;; customizing code here...
>   (doseq [ref references] (println ref))
>   `(ns ~name ~@references)
>   ;; ... and here
>   (println "hello CONS"))
> (intern 'clojure.core (with-meta 'co-ns {:macro true}) @#'co-ns)
> 
> and the interning bit isn't even necessary.  This makes it easy to add ref 
> keywords, e.g. (foo/co-ns (:require ...) (:my-ref ...))
> 
> Thanks,
> 
> Gregg
> 
> 
> 
> -- 
> 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.

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