Analyze Path FTW

June 10, 2015

In ClojureScript if you establish a REPL into a running JavaScript environment and require one of your namespaces, then the symbols become available. But, if you disconnect and reconnect, by default the REPL will have "forgotten" about the symbols, even though they have already been loaded into the JavaScript environment. But, there is a cool :analyze-path REPL option that eliminates this issue.




Here is an example, using the Ambly REPL. (This should also work with the browser REPL).

Let's say you have the following in src/foo/bar.cljs:

(ns foo.bar)

(defn square [x] 
  (* x x))

And in the REPL you do this:

cljs.user=> (require 'foo.bar)
nil
cljs.user=> (ns-interns 'foo.bar)
{square #'foo.bar/square}
cljs.user=> (foo.bar/square 3)
9

Now, with the iOS app still running, quit Ambly and reconnect and do this:

cljs.user=> (ns-interns 'foo.bar)
{}
cljs.user=> (foo.bar/square 3)
WARNING: Use of undeclared Var foo.bar/square at line 1 <cljs repl>
9

This shows that the function definition still exists, but that the new REPL instance is simply unaware that it has been loaded.

Now, let's use the :analyze-path feature. The transcript below additionally shows the forms being used to start up the REPL from the Clojure REPL, so that you can see where :analyze-path is being used.

user=> (require '[cljs.repl :as repl])
nil
user=> (require '[ambly.core :as ambly])
nil
user=> (repl/repl (ambly/repl-env) :analyze-path "src")

[1] Ambly Demo on iPhone Simulator (Mikes-MacBook-Pro)

[R] Refresh

Choice: 1

Connecting to Ambly Demo on iPhone Simulator (Mikes-MacBook-Pro) ...

To quit, type: :cljs/quit
cljs.user=> (ns-interns 'foo.bar)
{square #'foo.bar/square}
cljs.user=> (foo.bar/square 3)
9

Awesome. This is as it should be!

Tags: Ambly ClojureScript