如何在不带限定符的所有命名空间中使命名空间中的符号可访问? [英] How to make symbol(s) from namespace accessible in all namespaces w/o qualifier?

查看:88
本文介绍了如何在不带限定符的所有命名空间中使命名空间中的符号可访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有调试实用程序的名称空间,该实用程序仅在开发中使用.我想让它们在所有不带限定符的命名空间中均可访问(与clojure.core中的符号相同).

比方说我的项目结构如下:

dbg_utils.clj :

(ns project.dbg-utils)

(defmacro dbg ...)

db.clj

(ns project.db)

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

我想启动REPL并输入如下内容:

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

并且在不使用限定符的情况下使用dbg宏后:

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

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

是否有类似globalize-symbol(或接近此)的东西?

解决方案

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.

这篇关于如何在不带限定符的所有命名空间中使命名空间中的符号可访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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