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

标签 clojure

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

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/

相关文章:

clojure - 长期运行的递归 Clojure 函数的进度更新

clojure - Clojure 中与平台无关的数学函数(脚本)

clojure - Cascalog deffilterop 与纯 clojure

Clojure 1.3 中的函数性能

clojure - 为什么 clojure 的向量函数定义如此冗长?

clojure - 为什么这个循环函数比 map 慢?

postgresql - 在 Clojure 中使用 PGobject

amazon-web-services - 如何从 Clojure 项目或 jar 创建 AWS Lambda 函数?

macros - 强制扩展 Clojure 宏内的表达式

css - Clojure:CSS 的 DSL