为什么在使用in-ns移至新名称空间时丢失所有符号? [英] Why do I lose all symbols when using in-ns to move to a new namespace?

查看:105
本文介绍了为什么在使用in-ns移至新名称空间时丢失所有符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Leiningen REPL中运行以下代码:

Running the following code in a Leiningen REPL:

(in-ns 'my-namespace.core)
(+ 2 2)

导致此错误:

CompilerException java.lang.RuntimeException: Unable to resolve symbol: + in this context

为什么?

推荐答案

使用in-ns创建新的名称空间时,默认情况下不会引用核心名称空间(clojure.core). 引用"名称空间是指以某种方式将其包含在您的名称空间中,以便您可以将该名称空间的符号引用为自己的符号.

When you create a new namespace using in-ns, the core namespace (clojure.core) is not referred by default. "Referring" a namespace means including it in your namespace in such a way that you can refer to that namespace's symbols as your own.

仍然可以使用完全限定的名称来自clojure.core的符号,如下所示:

It is still possible to use symbols from clojure.core using fully qualified names, like so:

(clojure.core/+ 2 2)

解决方案是:

  1. 使用ns代替in-ns,如下所示:(ns my-namespace.core)
  2. 引用clojure.core,如下所示:(clojure.core/refer-clojure)
  1. Use ns instead of in-ns, like so: (ns my-namespace.core)
  2. Refer clojure.core, like so: (clojure.core/refer-clojure)

这篇关于为什么在使用in-ns移至新名称空间时丢失所有符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆