當我執行clojure.repl/source activatein repl 時,它給了我激活函式的來源。
現在,我在命名空間中定義了一個自定義函式tutorial.test
(ns tutorial.test)
(defn return_type_of_arg
[x]
(type x)
)
將 repl 命名空間切換到 repltutorial.test并加載此檔案后,我嘗試執行clojure.repl/source return_type_of_arg.
它給我的輸出為Source not found
怎么了?
編輯:
我已確認名稱空間已轉移到所需的名稱空間。請看下圖:

這些是我遵循的確切步驟:
- 創建了一個檔案
test.clj并將上述功能添加到其中。 - 在 Intellij Idea 中左鍵單擊此檔案并選擇選項
Switch REPL namespace to current file。 - 在 Intellij Idea 中左鍵單擊此檔案并選擇選項
Load file in REPL。
uj5u.com熱心網友回復:
下面是一個 REPL 會話的記錄,顯示了完成這項作業的步驟。
dorabs-imac:tmp dorab$ cat tutorial.clj
(ns tutorial)
(defn return_type_of_arg
[x]
(type x))
dorabs-imac:tmp dorab$ clj -Sdeps '{:paths ["."]}'
Clojure 1.10.3
user=> (require 'tutorial)
nil
user=> (in-ns 'tutorial)
#object[clojure.lang.Namespace 0x187eb9a8 "tutorial"]
tutorial=> (clojure.repl/source return_type_of_arg)
(defn return_type_of_arg
[x]
(type x))
nil
tutorial=>
dorabs-imac:tmp dorab$
在編輯中添加:
- 首先,我創建了一個
tutorial.clj使用ns表單和defn函式呼叫的檔案。 - 然后我使用 CLASSPATH 中的當前目錄 (
.) 啟動了一個 REPL,因此 Clojure 知道從哪里加載檔案。 - 然后我使用
require. 這會將函式定義加載到tutorial命名空間中。 - 然后我將 REPL 的當前命名空間更改為
tutorial使用該in-ns函式。 - 然后我運行了這個
clojure.repl/source函式。
進一步編輯:
我想我已經確定了您面臨的問題。似乎您使用 IntelliJ 的方式是將檔案的內容發送到 REPL,而不是requireing 檔案。根據檔案source,“列印給定符號的源代碼,如果它可以找到它。
這要求符號決議為在 .clj 位于類路徑中的命名空間中定義的 Var。” [強調我的]。因此,當檔案內容直接(不使用require)發送到 REPL 時,沒有.clj用于source查找源的檔案。以下成績單證明了這一點。將以下成績單與上述有效成績單進行比較。
dorabs-imac:tmp dorab$ clj
Clojure 1.10.3
user=> (ns tutorial)
(defn return_type_of_arg
[x]
(type x))
nil
tutorial=> tutorial=> #'tutorial/return_type_of_arg
tutorial=> tutorial=> (clojure.repl/source return_type_of_arg)
Source not found
nil
tutorial=>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/444984.html
