What are the alternatives to using remove-ns to clean up definitions

Pretty much, yes. Also, when I rename a function, I always do a (global) find on the old name to check I got all instances (and, of course, I’ll eval each changed form into the REPL as I make edits).

To be honest, I don’t find myself renaming functions very often – and I think that’s partly from “playing” with nearly every function via the REPL as I write them, so the names “evolve” to a stable state before I have calls to them elsewhere. In the same way that TDD tends to guide how you design a function’s name and signature – by focusing on usage, rather than implementation.

3 Likes

Neat trick.

I guess (def bla nil) works for all defonce Var?

3 Likes

defonce checks for a root binding and will not overwrite any Var that has one – so, no, the (def bla nil) trick won’t work with defonce, and you’ll have to ns-unmap it.

(it was a good question – I had to go experiment in the REPL to figure out the answer!)

1 Like

I use a function to walk all the namespaces and remove the mappings to the var:


It’s a bit complicated because it handles both Clojure and Clojurescript. A Clojure only version could be very much simplified.

2 Likes

I use remove-ns when I really can’t figure out why something isn’t working, just in case it’s an old binding. I don’t know if I’ve ever used clojure.tools.namespace.repl/refresh. I prefer a minimal toolchain. If I really get stuck, I just restart the REPL. That’s rare. My computer crashes more often than I reset the REPL.

Part of it is laziness. I put clojure.tools.namespace on my mental “check it out” list and just never got to it. But I guess that shows that I never needed it.

Two big reasons to use a tool like refresh are multimethods and protocols. If you redefine the protocol, you have to re-eval all extend-protocol forms, among other issues. But it strikes me that I also rarely use protocols and multimethods, so that may be why I’ve never needed it.

5 Likes

That script to stub test-ns for each namespace sounds really nice. Is that functionality included in tooling anywhere, or available to share?

Here’s the main body of it – you’ll need to set copyright and suffix prior to it:

last_stem=
for f in `find src -name '*.clj'`
do
  stem=`echo $f | sed 's;src/\(.*\)\.clj;\1;'`
  mkdir -p test/`dirname $stem`
  if test ! -f test/${stem}_${suffix}.clj
  then
    if test "$last_stem" != "$stem"
    then
      echo $stem
      last_stem=$stem
    fi
    (
      echo ";; copyright (c) 2019 ${copyright}"
      echo ''
      ns=`echo $stem | sed 's;/;.;g' | sed 's;_;-;g'`
      echo "(ns ${ns}-${suffix}"
      echo "  (:require [${ns} :refer :all]))"
    ) > test/${stem}_${suffix}.clj
  fi
done
1 Like

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