Ionuț G. Stan
Ionuț G. Stan

Reputation: 179219

Clojure: re-export vars

Is there a way to re-export some namespace's vars from another namespace? I'd like to be able to do this:

(ns mine.core
  (:use [incanter core charts datasets io]))

; re-export Incanter somehow

Then in the REPL I would be able to use Incanter's functions by just use-ing mine.core.

user=> (use 'mine.core)
nil
user=> (view (histogram (sample-normal 1000)))

Thanks!

Upvotes: 5

Views: 2464

Answers (3)

Petr Gladkikh
Petr Gladkikh

Reputation: 1942

I suggest to consider importing all necessary dependencies explicitly as suggested in other answers. This usually leads to less complex code.

However if you do want this, e.g. to present a consistent interface, then you can do this with clojure.core/intern function that makes any symbol "native" to given namespace. For example:

(ns user)
(intern 'user 'map clojure.core/map)    
(ns new-ns)
(require 'user)
(user/map inc [1 2 3])
; => [2 3 4]    
(use 'user)
; =>  WARNING: map already refers to: #'clojure.core/map in namespace: new-ns, 
;     being replaced by: #'user/map
(map inc [1 2 3]) 
; => [2 3 4]

Note that changes in symbol's original namespace will not be reflected until you re-intern the symbol.

Upvotes: 0

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91617

I used to accomplish this by putting the commonly REPLd use expressions in a separate file that I can load-file when I start my REPL. This worked fairly well because I could put everything in there and then get at more than just one namespace.

Later I switched to defining an 'everything' namespace and starting my repl in that using leiningend :main directive like in this SO question which was like my first approach but seemed more elegant.

my ways continued to change and i now always switch to the namespace containing the code. This has worked well because on larger project It helps keep track of what code goes where and I think this practice helps me learn the layout of the code faster. Of course everyone's experiences are different and personal, YMMV :)

Upvotes: 3

MHOOO
MHOOO

Reputation: 643

Try http://code.google.com/p/clj-nstools/

Upvotes: 1

Related Questions