如何打印所需的命名空间?

问题描述 投票:0回答:1

我需要命名空间,我可以使用它:

(ns core
  (:require [hello :as h]))

(println h/x)

但是为什么我不能只打印名称空间?

(println h) # Unable to resolve symbol: h in this context 

我用 deps-tools 和 leiningen 尝试过这个

clojure
1个回答
0
投票

当您说

(println h)
时,您要求打印当前命名空间中与符号
h
绑定的值。除非后面跟有
h
字符,否则
/
不会被解释为命名空间(或命名空间别名)。因此
(println h/x)
要求打印别名
x
引用的命名空间中与符号
h
绑定的值,换句话说,
hello.x
。您尚未将任何内容绑定到命名空间中的符号
h
,因此
h
本身是未定义的。

如果您想查看命名空间本身,请执行类似

(println (the-ns 'hello))
的操作(请参阅 https://clojuredocs.org/clojure.core/the-ns)。

© www.soinside.com 2019 - 2024. All rights reserved.