Web Development with Clojure, Third Edition: guestbook.auth functions not invocable (P. 168)

Dmitri Sotnikov @dmitri and Scot Brown @svmbrown

I was trying to execute the following code on p. 168:

user> (in-ns 'guestbook.auth)
#namespace[guestbook.auth]
guestbook.auth> (create-user! “testuser” “testpass”)
1
guestbook.auth> (authenticate-user “testuser” “testpass”)
{:login “testuser”, :created_at #inst “2019-09-20T02:22:53.217-00:00”}
guestbook.auth> (authenticate-user “testuser” “wrongpass”)

but the functions in the guestbook.auth namespace were simply unavailable after I switched to that namespace. I was getting No such var error messages.

After a lot of searching, I stumbled upon this from https://clojure.org/guides/repl/navigating_namespaces

How to make sure a lib is loaded

To make sure that a lib with namespace mylib.ns1 has been loaded in the REPL, you can do any one of the following:

  1. require it directly: (require '[mylib.ns1])
  2. load a namespace which itself requires mylib.ns1 (directly or indirectly).
  3. evaluate manually all the code in the source file mylib.ns1

So I believe this section in the book was too premature. I actually went on to type in the login route code further down on pp. 168-9. Since that login route code loads (a la #2 above) the guestbook.auth namespace, once you jack-in or lein repl you will be able to execute the functions in the guestbook.auth namespace. So I think if the code blocks are reversed, that would help solve this issue.

Yeah, using require would force the namespace to load. Simply doing in-ns will switch to the namespace, but won’t trigger for the functions there to be evaluated. I think you’re right that loading the namespace would be the way to go here to avoid the behavior.