Search code examples
clojurefunctional-programmingjvmleiningencider

Get 'Attempting to call unbound fn' in clojure command line app


I have the following code and it works when I go through and step through it in Cider, but when I run it straight through on the command line I get the following error:

Exception in thread "main" java.lang.IllegalStateException: Attempting to call unbound fn: #'clojure-todo.todo/execute-todo-cmd, compiling:(/private/var/folders/d_/b8gmsvl16sgdg70m15gx20l40000gn/T/form-init6080372317525706515.clj:1:125)
        at clojure.lang.Compiler.load(Compiler.java:7391)
        at clojure.lang.Compiler.loadFile(Compiler.java:7317)
        at clojure.main$load_script.invokeStatic(main.clj:275)
        at clojure.main$init_opt.invokeStatic(main.clj:277)
        at clojure.main$init_opt.invoke(main.clj:277)
        at clojure.main$initialize.invokeStatic(main.clj:308)
        at clojure.main$null_opt.invokeStatic(main.clj:342)
        at clojure.main$null_opt.invoke(main.clj:339)
        at clojure.main$main.invokeStatic(main.clj:421)
        at clojure.main$main.doInvoke(main.clj:384)
        at clojure.lang.RestFn.invoke(RestFn.java:421)
        at clojure.lang.Var.invoke(Var.java:383)
        at clojure.lang.AFn.applyToHelper(AFn.java:156)
        at clojure.lang.Var.applyTo(Var.java:700)
        at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalStateException: Attempting to call unbound fn: #'clojure-todo.todo/execute-todo-cmd

Here is the code. I guess it's a namespace error, but I'm confused as to how it could be a namespace issue if it's working in cider.

 (ns clojure-todo.todo
  (:gen-class))

    ///// Other Code here
(defn print-msg-list [list]
  (doseq [item list] (print-msg item)))

(defn create-todo [todo-string] (hash-map :todo-content todo-string :checked false))

(defn list-todos [todos]
  (doseq [single-todo todos]
    (do
      (print (get single-todo :todo-content))
      (let [is-checked (get single-todo :checked)]
        (if is-checked
          (print-msg :is-checked)
          (print-msg :is-unchecked))))))

(defn add-todo []
  (do
    (print-msg :add-todo-message)
    (let [todo-string (read-line) the-todo-created (create-todo todo-string)]
        (def the-todos (conj the-todos the-todo-created))
        (list-todos the-todos)))

(def todo-fns {"1" add-todo "2" delete-todo "3" list-todos "4" check-todo})

(defn execute-todo-cmd [command]
  (get todo-fns command nil)))

(defn take-todo-command [backup-fn]
  (let [command (read-line)]
      (if (= command "q")
        (print-msg :goodbye)
        (let [todo-fn (execute-todo-cmd command)]
          (if todo-fn
            (todo-fn)
            (do
              (print-msg :sorry)
              (print-msg :border)
              (backup-fn)))))))

(defn run-app []
  (do
    (print-msg :welcome)
    (print-msg-list '(:add-todo :list-todos :delete-todo :check-todo :quit))
    ;; The take-todo-command fn takes a backup in case the user types in
    ;; a command that's not supported by the application. In this case we just
    ;; want the app to restart
    (take-todo-command run-app)))

Solution

  • This was just missing a close paren in add-todo, and there is an extra close paren in execute-todo-cmd. So execute-todo-cmd does not exist until add-todo is run, which is why it yields an unbound-fn error.