【问题标题】:Why does `dir` not work with current namespace gotten from "current namespace var" - `*ns*`为什么`dir`不适用于从“当前命名空间var”获取的当前命名空间 - `*ns*`
【发布时间】:2021-09-07 23:27:18
【问题描述】:

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

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')

【问题讨论】:

    标签: clojure


    【解决方案1】:

    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

    【讨论】:

      猜你喜欢
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多