8

When executing this function from lein run, the program executes as expected. But I am trying out atom.io's proto-repl package and when I call the function using the proto-repl, it gives a "CompilerException java.lang.RuntimeException: Unable to resolve symbol: can-vote in this context." Here is my function:

(defn can-vote
  []
   (println "Enter age: ")
   (let [age (read-line)]
    (let [new-age (read-string age)]
     (if (< new-age 18) (println "Not old enough")))
      (println "Yay! You can vote")))
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Mahmud Adam
  • 3,489
  • 8
  • 33
  • 54
  • 1
    What namespace is this function defined in? – Josh Feb 01 '16 at 20:18
  • (ns clojure-noob.core (:gen-class)) So I highlight the entire function in the left-pane of the editor, the proto-repl on the right updates to the clojure-noob core namespace, but when I try calling the function thereafter to watch it execute, it throws this exception. – Mahmud Adam Feb 01 '16 at 20:21
  • If I paste the function directly into the REPL it seems to execute but does not allow me to enter any text at the prompt when calling the (can-vote) function – Mahmud Adam Feb 01 '16 at 20:31
  • That's a different problem, see here -> http://stackoverflow.com/questions/7707558/clojure-read-line-doesnt-wait-for-input – Josh Feb 01 '16 at 20:35

1 Answers1

15

When your repl starts, it likely puts you in the user namespace. You either need to move to your clojure-noob.core namespace or call it with the fully qualified symbol.

If you want to switch namespaces

(ns clojure-noob.core) ;; switch to the correct namespace
(can-vote) ;; call the function

If you want to call it with the fully qualified symbol from the user namespace

(require 'clojure-noob.core) ;; first require the namespace
(clojure-noob.core/can-vote) ;; call the fully qualified function

You can read much more about namespaces and calling functions from other namespaces and libraries here.

Josh
  • 5,631
  • 1
  • 28
  • 54