Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make clojure functions available in every namespace during development?

Tags:

clojure

I have number of useful helper functions that I use at the REPL during Clojure development. This includes some built-in functions like doc and pprint, but also some custom ones of my own from my user.clj. The default clojure experience seems to be:

dev> (doc +)
;; works
dev> (in-ns 'project.core)
project.core> (doc +)
;; what is this "doc" thing you're talking about!?!?

which is pretty irritating (I'm aware that I can refer to clojure.repl/doc here). Is there an easy way to ensure that something is available during development regardless of which namespace I'm currently operating in?

like image 734
Derek Thurn Avatar asked Nov 07 '22 23:11

Derek Thurn


2 Answers

One option is to add a :repl-options to your project map in project.clj:

(defproject myproj "1.0"
  :dependencies [[org.clojure/clojure "1.9.0-alpha15"]]
  :repl-options { :init-ns myproj.core
                  :init (require '[clojure.repl :refer :all]) })

...

$ lein repl
myproj.core=> (doc +)   ; works
like image 164
Josh Avatar answered Nov 15 '22 13:11

Josh


As Alex Gherega mentions, it's probably a bad idea to automatically and indiscriminately import stuff into every namespace. However, you could create a macro (or maybe a function would work?) to help import development-related namespaced when/if they are needed. Add it to your user.clj. Then if you are working in another namespace and decide it would be useful, you can just run (user/import-useful-dev-stuff) (or whatever you decide to call it).

For what it's worth, I pretty much do all my REPL sessions within the user namespace and require all the namespaces I need from there. I use Emacs, and if I need to change something in another namespace, I just change the source, then do C-c C-k (after initiating a cider session) to reload the file. If you don't use Emacs, it's only a little more work to reload from the REPL via require with the :reload option.

like image 24
Nathan Davis Avatar answered Nov 15 '22 13:11

Nathan Davis