且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不带限定符的所有命名空间中使命名空间中的符号可访问?

更新时间:2023-11-02 16:12:52

Leiningen为此提供了:injections功能和:user配置文件.

本文分享了一些有关如何操作的提示那.它基本上是通过将所需的调试功能添加到clojure.core来工作的,并且由于使用ns宏时,始终包含此名称空间中的所有公共var(除非另行指定),因此您将在所有名称空间中使用它们. /p>

I have namespace with debug utilities that are used only in development. I'd like to make them accessible in all namespaces without qualifier (same as symbols from clojure.core).

Let's say my project structure is as follows:

dbg_utils.clj:

(ns project.dbg-utils)

(defmacro dbg ...)

db.clj

(ns project.db)

(defn create-entity [...]
  ...)

After I'd like to fire up REPL and type something like this:

> (require 'project.db)
> (require 'project.dbg-utils)
> (globalize-symbol 'project.dbg-utils/dbg)

And after use dbg macro without qualifier:

(ns project.db) ;; no require of project.dbg-utils

(defn create-entity [...]
  (dbg ...) ;; and no qualifier here
  ...)

Is anything like globalize-symbol (or close to this) available?

Leiningen provides the :injections feature and the :user profile for that.

This article shares some pointers on how to do that. It basically works by adding the debugging functions you want to clojure.core and since all public vars from this namespace are always included when using the ns macro (unless you specify otherwise), you will have them available in all your namespaces.