为什么 clojure.repl/source 不适用于我的自定义函数
当我在 repl 中执行 clojure.repl/source activate 时,它会提供 activate 函数的源代码。
现在,我在命名空间 tutorial.test
中定义了一个自定义函数。
(ns tutorial.test)
(defn return_type_of_arg
[x]
(type x)
)
将 repl 命名空间切换到 tutorial.test
并在 repl 中加载此文件后,我尝试执行 clojure .repl/source return_type_of_arg
。
它给我的输出为 Source not found
出了什么问题?
编辑:
我已经确认名称空间已转移到所需的名称空间。请参见下图:
这些是我遵循的具体步骤:
- 创建一个文件
test.clj
并将上述功能添加到其中。 - 在 Intellij Idea 中左键单击此文件,然后选择选项
将 REPL 命名空间切换到当前文件
。 - 在 Intellij Idea 中左键单击此文件并选择选项
在 REPL 中加载文件
。
When I execute the clojure.repl/source activate
in repl, it gives me the source of the activate function.
Now, I defined a custom function in namespace tutorial.test
(ns tutorial.test)
(defn return_type_of_arg
[x]
(type x)
)
After switching the repl namespace to tutorial.test
and loading this file in repl, I tried to execute clojure.repl/source return_type_of_arg
.
It's giving me the output as Source not found
What's going wrong?
EDITS:
I have confirmed that the namespace is shifted to the one required. Please see the following image:
These are the exact steps I followed:
- Created a file
test.clj
and added above fucntion into it. - Left clicked this file in Intellij Idea and selected option
Switch REPL namespace to current file
. - Left clicked this file in Intellij Idea and selected option
Load file in REPL
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面是 REPL 会话的记录,显示了完成这项工作的步骤。
在编辑中添加:
tutorial.clj
的文件,其格式为ns
,其defn
为功能。.
) 启动 REPL,以便 Clojure 知道从哪里加载文件。require
加载该文件。这会将函数定义加载到tutorial
命名空间中。in-ns
函数将 REPL 的当前命名空间更改为tutorial
。进一步编辑:
我认为我已经确定了您面临的问题。看来您使用 IntelliJ 的方式是将文件的内容发送到 REPL,而不是
需要
文件。根据source
的文档,“如果可以找到给定符号,则打印它的源代码。这要求符号解析为在 a 中定义的 Var
.clj 位于类路径中的命名空间。”[强调我的]。因此,当文件内容直接发送到 REPL(不使用
require
)时,没有.clj
文件用于source
查找源代码 下面的脚本演示了这一点。Below is a transcript of a REPL session that shows the steps to make this work.
Added in edit:
tutorial.clj
with thens
form and thedefn
of the function..
) in the CLASSPATH so Clojure knows where to load the file from.require
. This loads the function definition into thetutorial
namespace.tutorial
using thein-ns
function.clojure.repl/source
function.Further edit:
I think I have identified the problem you are facing. It seems that the way you are using IntelliJ, it is sending the contents of the file to the REPL, rather than
require
ing the file. According to the docs forsource
, "Prints the source code for the given symbol, if it can find it.This requires that the symbol resolve to a Var defined in a
namespace for which the .clj is in the classpath." [Emphasis mine]. So, when the file contents are sent to the REPL directly (without using
require
) there is no.clj
file forsource
to look for the source. The following transcript demonstrates that. Compare the following transcript with the transcript above that does work.