This video is available to students only

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.

Clojure is a hosted language. In our case, ClojureScript is hosted on JavaScript (Node or the browser). This means that at any given moment, there are two sets of code available: CLJS code that we wrote and the JS code that the build tool produced.

To test our code, we need the JS version to run. But modifications are made to the CLJS version. The REPL provides a bi-directional bridge between these two realms. It lets you control the execution of code in the JavaScript realm, without leaving the ClojureScript realm.

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:

Inline Evaluation 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.

Our 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.

Scope of REPL
  1. you write ClojureScript code in your text editor (we already have the source for first-project)

  2. Shadow watch converts your CLJS code to valid JavaScript code (can be run using yarn shadow-cljs watch :script)

  3. 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.js in another terminal)

  4. 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 watch mode?

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.

 

This page is a preview of Tinycanva: Clojure for React Developers

Please select a discussion on the left.