5

I have this save-message function in guestbook.models.db namespace and I am trying to run it in the repl but I get this:

guestbook.models.db> (save-message "A" "Hi"
                                   )
CompilerException java.lang.RuntimeException: Unable to resolve symbol: save-message in this context, compiling:(/private/var/folders/xc/ypy3lqhj08xg2hjc6g81qwl80000gn/T/form-init7598384514150426113.clj:1:1) 

I reload and try again and I get the same error

guestbook.models.db> (:reload 'guestbook.models.db)
nil
guestbook.models.db> (save-message "A" "Hi"
                                   )
CompilerException java.lang.RuntimeException: Unable to resolve symbol: save-message in this context, compiling:(/private/var/folders/xc/ypy3lqhj08xg2hjc6g81qwl80000gn/T/form-init7598384514150426113.clj:1:1) 
guestbook.models.db> 

What am I doing wrong?

1 Answer 1

6

You want to say

(require :reload 'guestbook.models.db)

This reloads this single namespace; if you use :reload-all instead, it will also recursively reload all namespaces loaded by guestbook.models.db, directly or indirectly.

See (doc require) for details.


As for (:reload 'guestbook.models.db):

Keywords in Clojure, when used as functions, attempt to treat their first argument as a map to look themselves up in. For example, (:foo {:foo 1}) returns 1. If this argument cannot be so treated, nil is returned. Optionally a second argument can be provided, to be returned as the default value in case the keyword fails to find a value corresponding to itself in its first argument (whether it happens to be a non-map or a map which has no entry for this key).

Sign up to request clarification or add additional context in comments.

6 Comments

I get the same error when I try (require :reload 'guestbook.models.db): guestbook.models.db> (require :reload 'guestbook.models.db) CompilerException java.lang.RuntimeException: Unable to resolve symbol: require in this context, compiling:(/private/var/folders/xc/ypy3lqhj08xg2hjc6g81qwl80000gn/T/form-init7598384514150426113.clj:1:1)
This one is about require. How did you switch to guestbook.models.db in the REPL? in-ns or something editor-specific by any chance? You need to (require 'guestbook.models.db) at the REPL before switching to it with in-ns (or use a function of your editor that loads the namespace properly before switching to it), otherwise you'll switch to a newly created namespace of this name without the basic scaffolding such as clojure.core being pulled in, which clearly happened here.
Oh, you should be able to fix this by saying (clojure.core/require :reload 'guestbook.models.db).
You must have used in-ns (or some editor feature that relies on it directly) to switch to guestbook.models.db without loading it from the source file. in-ns creates completely bare namespaces which don't even refer to symbols from clojure.core (see (doc refer)) and is not interested in source files at all. Instead you should first load your source file (for example using require). Once the namespace is loaded, in-ns can be used to switch to it.
I'm seeing similar behavior when loading the namespace like this: (ns guestbook.repl): (start-server) CompilerException java.lang.RuntimeException: Unable to resolve symbol: start-server in this context, compiling:(C:\Users\Simeon\AppData\Local\Temp\form-init6442755879023838607.clj:1:1) . Does it also behaves similar to in-ns and needs a requre command? Would (use guestbook.repl) have the same effect? Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.