NREPL and Inline Evaluation
The REPL is an essential element of a Lisper's toolkit. In this chapter, we'll solidify our grasp of the REPL and editor integration. We'll also learn about some problems that come with the REPL.
nREPL and inline evaluation#
REPL stands for Read (source string), Eval (source string), Print (result), and Loop. nREPL or short for Network REPL is a REPL that runs in client-server mode. We use the terms REPL and nREPL interchangeably.
According to the official Clojure docs, the REPL is a user interface to your program.
Inline evaluation demo#
Inline evaluation is a niche concept. Instead of building the concept from the bottom up, let's see the final product and tear it down from the top. We are going to use the
first-project with Shadow for this demo:
Notice how we evaluate the code inline. Inline Evaluation leverages the nREPL to execute code inline. This lets us develop and debug functions without leaving the editor. Inline evaluation is not limited to core functions. In the demo, we defined a new function, evaluated the
defn so it was available in the namespace, and then called that function inline. You can also evaluate third-party libraries, refer to inline docs, and a lot more. Inline evaluation is a Lisper's swiss army knife.
Something like React's Hot Reload compiles your code each time you save, so the newer version is available in your browser. Inline evaluation lets you completely bypass the browser (or runtime) and lets you evaluate your code right in your editor.
first-project was configured to run as a Node script, so the inline execution of the code happens in a Node env, and the REPL serves as a bridge. In this chapter, we'll connect the REPL to your editor.
Scope of the REPL#
We have introduced multiple tools to our process so far. Let's see how everything fits into the big picture.
you write ClojureScript code in your text editor (we already have the source for
yarn shadow-cljs watch :script)
you can execute a command that will run the JS code in the Node environment, as we did earlier (with
node build/node-script/core.jsin another terminal)
Node (or runtime) executes the code and exits (or not)
Do you recall that the Node script did not exit while Shadow was running in
This is because the REPL needs the runtime for inline evaluation. The REPL is a bridge between the CLJS source and the runtime. Where Shadow converts your code to be ready for the runtime, the REPL can initiate the execution, update functionality without full rebuilds and get the output from runtime back to the editor. This is how the REPL acts as a user interface for your program.
Connecting your editor to the nREPL#
If you followed the steps in the last section, you'll have your source open in your editor, Shadow
watch in a terminal, and the Node script in another terminal.
We have configured nREPL to run on port 9000, so let's connect our editor to it. We will talk about multiple editors, but you only need to follow the section that applies to your editor of choice.