为什么 clojure.repl/source 不适用于我的自定义函数

发布于 2025-01-13 14:28:02 字数 864 浏览 7 评论 0原文

当我在 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

出了什么问题?

编辑:

我已经确认名称空间已转移到所需的名称空间。请参见下图:

在此处输入图像描述

这些是我遵循的具体步骤:

  1. 创建一个文件 test.clj 并将上述功能添加到其中。
  2. 在 Intellij Idea 中左键单击此文件,然后选择选项将 REPL 命名空间切换到当前文件
  3. 在 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:

enter image description here

These are the exact steps I followed:

  1. Created a file test.clj and added above fucntion into it.
  2. Left clicked this file in Intellij Idea and selected option Switch REPL namespace to current file.
  3. Left clicked this file in Intellij Idea and selected option
    Load file in REPL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2025-01-20 14:28:02

下面是 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$ 

在编辑中添加:

  1. 首先,我创建了一个名为 tutorial.clj 的文件,其格式为 ns ,其 defn 为功能。
  2. 然后我使用 CLASSPATH 中的当前目录 (.) 启动 REPL,以便 Clojure 知道从哪里加载文件。
  3. 然后我使用 require 加载该文件。这会将函数定义加载到tutorial 命名空间中。
  4. 然后,我使用 in-ns 函数将 REPL 的当前命名空间更改为 tutorial
  5. 然后我运行了 clojure.repl/source 函数。

进一步编辑:

认为我已经确定了您面临的问题。看来您使用 IntelliJ 的方式是将文件的内容发送到 REPL,而不是需要文件。根据 source 的文档,“如果可以找到给定符号,则打印它的源代码。
这要求符号解析为在 a 中定义的 Var
.clj 位于类路径中的命名空间
。”[强调我的]。因此,当文件内容直接发送到 REPL(不使用 require)时,没有 .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=> 

Below is a transcript of a REPL session that shows the steps to make this work.

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$ 

Added in edit:

  1. First I created a file called tutorial.clj with the ns form and the defn of the function.
  2. Then I started a REPL with the current directory (.) in the CLASSPATH so Clojure knows where to load the file from.
  3. Then I loaded up the file using require. This loads the function definition into the tutorial namespace.
  4. Then I changed the current namespace of the REPL to be tutorial using the in-ns function.
  5. Then I ran the 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 requireing the file. According to the docs for source, "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 for source to look for the source. The following transcript demonstrates that. Compare the following transcript with the transcript above that does work.

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=> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文