Why use cider-send-ns-form-to-repl instead of cider-send-function-to-repl in Emacs Cider?

Is there any difference between cider-send-ns-form-to-repl and cider-send-function-to-repl?

For example, I have the following form in my clj file:

(ns e01.rebl-examples
  (:import java.io.File)
  (:require [clojure.java.io :as io]
            [clojure.core.protocols :as p]))

I can send this form to repl using both commands. I’d prefer cider-send-function-to-repl because I can use it with any form.

1 Like

cider-send-ns-form-to-repl will send the ns form no matter where your cursor is in the file.

1 Like

Does Cider also send the ns form when evaluating any form?

No, it doesn’t.

Yes and no.

There’s the send to repl functions. And there’s the eval functions.

The send to repl functions are like if you copy/pasted things in the REPL window and pressed enter. So they are executed in the context of your REPL window. So if you send some defn form it will just send that. It won’t send anything else.

The eval functions evaluate the form within the context of the first ns form found backward from the form you are evaluating. So if you evaluate some defn and above the defn you have (ns foo) and your REPL window is in the user namespace, Cider will switch to foo and evaluate the defn, but it’s not switching your REPL window, in the REPL window you are still going to be inside the user namespace.

The output of the send to repl functions will show in the REPL window.

The output of the eval functions will show inline with the code.

Yes, forms in an ns are evaluated in that ns, but the ns form itself is not evaluated if you don’t do it explicitly (so requires and imports are not available until you do)

1 Like

Ya, I don’t remember what it does exactly, I had looked it up a while ago. I think it might just call in-ns or maybe it just binds *ns*. If it does the former, it would create the namespace if it didn’t exist, but otherwise would just switch to it without requiring or importing anything.

I’m too lazy to go dig into the code now, but it looks like it’s calling in-ns.

(ns toto) ;; never evaluated from the buffer

(defn add [a b]
  (+ a b))
;; => #'toto/add