簡體   English   中英

在 clojure 命令行應用程序中獲取“嘗試調用未綁定的 fn”

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

我有以下代碼,當我在 Cider 中執行並逐步執行它時,它可以工作,但是當我直接在命令行上運行它時,我收到以下錯誤:

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

這是代碼。 我想這是一個命名空間錯誤,但我很困惑,如果它在蘋果酒中工作,它可能是一個命名空間問題。

 (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)))

這只是在 add-todo 中缺少一個關閉括號,並且在 execute-todo-cmd 中有一個額外的關閉括號。 所以在 add-todo 運行之前 execute-todo-cmd 不存在,這就是它產生 unbound-fn 錯誤的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM