简体   繁体   English

为什么Clojure在创建新命名空间后不提供标准库?

[英]Why Doesn't Clojure provide standard library after creating new namespace?

I came across this problem after creating a new namespace. 创建新的命名空间后,我遇到了这个问题。

Here is the code: 这是代码:

(create-ns 'my-new-ns)
=> #object[clojure.lang.Namespace 0x7c7c8359 "my-new-ns"]

(in-ns 'my-new-ns)
=> #object[clojure.lang.Namespace 0x7c7c8359 "my-new-ns"]

(reduce + [1 2 3])
CompilerException java.lang.RuntimeException: Unable to resolve symbol: reduce in this context, compiling:(/private/var/folders/pg/bynypsm12nx1s4gzm56mwtcr0000gn/T/form-init1425759900088902804.clj:1:1) 

As you can see reduce function is not defined in my-new-ns namespace. 如您所见, my-new-ns命名空间中未定义reduce函数。

I should be able to create new namespaces so What would be the best solution for this problem? 我应该能够创建新的命名空间,那么这个问题的最佳解决方案是什么?

PS: Also, I'm trying to create those namespaces for my users so they will be able to do whatever they want in their namespaces(the idea is like a container) and creating isolation between namespaces. PS:此外,我正在尝试为我的用户创建这些命名空间,以便他们能够在他们的命名空间中做任何他们想做的事情(这个想法就像一个容器)并在名称空间之间创建隔离。

clojure.core functions are not special in their need to be referred to make them available for unqualified use. clojure.core函数并不特殊,因为它们需要被引用以使它们可用于不合格的使用。 The ns macro does several things: ns宏做了几件事:

  • creates the namespace - create-ns 创建命名空间 - create-ns
  • changes the current namespace to that namespace - in-ns 将当前命名空间更改为该命名空间 - in-ns
  • automatically refers all of the clojure.core vars into the new namespace - refer-clojure 自动将所有clojure.core变量refer-clojure到新命名空间 - refer-clojure

You can always use the qualified form of the core function (unqualified is just less typing), so when you get in this situation, this simple call will get you right again: 你总是可以使用核心功能的合格形式(不合格只是少打字),所以当你遇到这种情况时,这个简单的通话会让你再次正确:

(clojure.core/refer-clojure)

Instead of creating namespace manually and then switching to it, I'd recommend using ns macro. 我建议使用ns宏,而不是手动创建命名空间然后切换到它。 According to the doc: 根据文件:

Sets *ns* to the namespace named by name (unevaluated), creating it if needed. 将* ns *设置为由name(未评估)命名的命名空间,并在需要时创建它。

Also it will load all public vars from clojure.core to newly created namespace. 它还会将所有公共变量clojure.core 加载到新创建的命名空间。

So, basically this 所以,基本上这个

> (create-ns 'my-new-ns)
> (in-ns 'my-new-ns)
> (clojure.core/refer 'clojure.core)

is equal to this 等于这个

> (ns my-new-ns)

Update: 更新:

Answering your question: symbols from standard library are not refer red in newly created namespace, that's why you cannot access them without qualifier: 回答你的问题:标准库中的符号在新创建的命名空间中不会refer红色,这就是为什么没有限定符就无法访问它们的原因:

> (create-ns 'x)
> (in-ns 'x)
> reduce ;; throws "Unable to resolve symbol"
> clojure.core/reduce ;; works fine

You need to refer those symbols manually by calling (clojure.core/refer 'clojure.core) . 您需要通过调用(clojure.core/refer 'clojure.core)手动引用这些符号。

Your new namespace needs to use the "standard" namespaces to be able to resolve names in them. 您的新命名空间需要使用“标准”命名空间才能解析其中的名称。 The documentation indicates that this would be java.lang , clojure.lang.Compiler and clojure.core . 文档表明这将是java.langclojure.lang.Compilerclojure.core

I guess you are supposed to use 我猜你应该用

(ns my-new-ns)

instead. 代替。 create-ns is a low-level facility. create-ns是一个低级设施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM