2

Strange problem, I know. So lein can be instructed to load a specific namespace when running the REPL with lein repl. That's great, let's assume I have a file called ns1.clj, so my project.clj file contains the line:

:repl-options {:init-ns ns1}

And, as expected, that file is loaded. However, I want to switch to another namespace (ns2) after ns1.clj does it's job, so I append the following to ns1.clj:

(ns ns2)

The problem is that Leiningen resets the REPL namespace to ns1 after the ns1.clj has finished. Is there any way to start the REPL by loading ns1.clj but not resetting the namespace post-load? By the way, I would assume that Leiningen should just execute the script and not set the namespace explicitly.

Background: I want to load a clj script and then switch to a namespace that has been loaded from an external source by that very script. So the logic in ns1.clj figures out what namespace should the REPL start in.

2
  • I think you are perhaps approaching your problem from the wrong direction. What you are attempting to do is a very unusual workflow. Don't think of *.clj files as scripts - there more like packages in java. Normally you would have an initial namespace i.e core.clj with :require statements for your other namespaces and then call the functions in the other namespaces from core.clj rather than try and call and load a namespace directly from within your main cli file. Commented Jan 29, 2015 at 1:17
  • I know that it's unusual but there's a reason why I want to do this -- I'm working on extending the REPL with some extra functionality. For example, I want to instrument/wrap the namespace switching in the REPL (so I want some functions to be imported into the current namespace, etc.) It is not necessarily a good idea, but it's more like an experiment now on what can be done with the REPL. Commented Jan 29, 2015 at 8:37

1 Answer 1

1

Based on your goal of wanting to extend repl functionality, I think you might do better to look at creating a lien plugin. You can then use this plugin without needing to modify your code and through your lein profiles, only have it installed when needed.

You might get some good ideas/pointers by looking at a number of existing lein plugins. In particular, there was quite an interesting blog post on planet clojure talking about a plugin called 'ultra' which extends the repl by adding ansi colours, improved test output formatting, enhanced stacktrace display and a few other repl tweaks. The project can be found on githug at https://www.github.com/venantius/ultra

Sign up to request clarification or add additional context in comments.

1 Comment

Actually at the moment I'm thinking on just simply leaving lein, and going with clojure.main/repl. By the way, I know Ultra and it's not compatible with what I'm doing right now (I need to do odd thinks like redefining defn...) :) Thanks though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.