Make clojure functions available in every namespace at design time?

I have a number of useful helper functions that I use in the REPL during Clojure development. This includes some built-in functions like doc

and pprint

as well as some of its own from mine user.clj

. Clojure is used by default:

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

      

which is pretty annoying (I know I can link to clojure.repl/doc

here). Is there an easy way to make sure something is available at design time, no matter what namespace I'm currently working in?

+3


source to share


4 answers


One option is to add :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

      

+1


source


Unless you are using upload or lane or some other form that will magically add this namespace to your REPL development session, I'm afraid you just have to manually require it every time.

The namespace thing has a well thought out role in avoiding name collisions. If you had another way to tell the current namespace to use / require a different namespace without doing so, then you would have many other problems.



If you are not using lein / boot, you can just do as Alex Miller answered . For a more controlled way that you need from clojure.repl or some other ns API, there is only a declaration require

and use

in the namespace you are interested in, for example:

(ns 'project.core
   (:require [clojure.repl :refer [doc]]))

      

0


source


As Alex Gerega mentions, it is probably a bad idea to automatically and indiscriminately import stuff into each namespace. However, you can create a macro (or maybe a function will work?) To help import development-related names if needed / if needed. Add it to yours user.clj

. Then, if you're working in a different namespace and you decide that would be useful, you can just run (user/import-useful-dev-stuff)

(or whatever you choose to name).

For what it's worth, I pretty much run all my REPL sessions in the namespace user

and require

all the namespaces I need from there. I am using Emacs and if I need to change something in a different namespace I just change the source and then C-c C-k

(after starting a cider session) to reload the file. If you are not using Emacs, reloading the REPL require

with an option :reload

takes a little more work.

0


source


If you find yourself in this situation, follow these steps:

(clojure.core/refer-clojure)

      

claim and load everything from clojure.core.


Or an alternative approach, if exists project.core

, instead:

(require 'project.core)
(in-ns 'project.core)

      

Loading the namespace with require

will reference clojure.core.


Or, if it project.core

doesn't exist, do:

(ns project.core)

      

The macro ns

will execute both refer-clojure

and in-ns

.

-1


source







All Articles