(dir (ns-name *ns*)) Execution error (ClassCastException) at user/eval2010 -6ren">
gpt4 book ai didi

clojure - 为什么 `dir` 不适用于从 "current namespace var"- `*ns*` 获取的当前命名空间

转载 作者:行者123 更新时间:2023-12-02 18:33:42 24 4
gpt4 key购买 nike

或者,换句话说,为什么这不起作用:

user=> (dir (ns-name *ns*))
Execution error (ClassCastException) at user/eval2010 (REPL:1).
class clojure.lang.PersistentList cannot be cast to class clojure.lang.Symbol (clojure.lang.PersistentList and clojure.lang.Symbol are in unnamed module of loader 'bootstrap')

最佳答案

dir 是一个宏,需要一个不带引号的符号作为其参数。它应该像这样使用:


user=> (clojure.repl/dir clojure.string)
blank?
capitalize
escape
join
lower-case
replace
...

当你这样调用它时:

(ns demo.core
(:require [clojure.repl :as repl]))

(println (repl/dir (ns-name *ns*)))

您没有传递一个不带引号的符号(例如clojure.string),而是传递一个以符号ns-name作为其第一个元素的列表。

正如 clojure.repl/dir 的命名空间所暗示的那样,此命令旨在手动键入 REPL,而不是以编程方式使用。


如果您确实想以编程方式获取信息,您可能需要更像以下之一的东西:

(ns-publics 'tst.demo.core)
(ns-publics (ns-name *ns*))

两者都有效。

请务必仔细阅读Clojure CheatSheet还有这个list of documentation .

关于clojure - 为什么 `dir` 不适用于从 "current namespace var"- `*ns*` 获取的当前命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69095460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com