Is it possible to "clone" a namespace?

Lets say I have a namespace declaration like this:

(ns my.core
  (:require [other.stuff :as stuff]))

(defn a [] (stuff/x))

I’ve figured that it could be neat to create a “duplicate” namespace when debugging, and the most basic way would be to do something like this:

(ns my.debugging)
(use 'my.core)
(a)

So far so good. But I’d also like to duplicate the require-statement of my.core, since this won’t work (in my.debugging):

(stuff/x)

Is there a way to get all the dependencies of a namespace (programatically) copied into the current namespace, in such a way that I can use the same aliases e.t.c.?

1 Like
(doseq [[sym ns] (ns-aliases (find-ns 'clojure.datafy))]
  (alias sym (symbol (str ns))))

(p/datafy 1)

Using ns-aliases and alias, you can write a function that imports same aliases as ones defined in other ns.

1 Like

Also, why duplicate instead of just working in that ns with in-ns?

(in-ns 'clojure.datafy)

(p/datafy 1)
1 Like

Cool, almost there. What’s missing now is when a ns is declared like so:

(ns my.core
  (:require [other.stuff :refer :all]))

Is there a way to import those too?

With regards to “why?”, the reason is that through https://github.com/Saikyun/miracle.save I currently def in the current namespace. If it was easy to “clone” it, I wouldn’t pollute the current namespace. :slight_smile:

I believe you can do this with: https://github.com/ztellman/potemkin#import-vars

Just a thought, what do you think about ld being a macro with saved local bindings instead of defining new vars?
Like that:

(defn add [x y] (save :a) (+ x y))
(add 5 10)

(let-ld :a x) ;; => 5
(let-ld :a (class y)) ;; => Long

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.