使用 clojure.string 会导致警告

发布于 2025-01-06 14:08:35 字数 441 浏览 0 评论 0原文

使用 clojure.string 时,我的 clojure 脚本收到以下警告

WARNING: replace already refers to: #'clojure.core/replace in namespace: tutorial.regexp, being replaced by: #'clojure.string/replace
WARNING: reverse already refers to: #'clojure.core/reverse in namespace: tutorial.regexp, being replaced by: #'clojure.string/reverse

(ns play-with-it
  (:use [clojure.string]))

有没有办法修复这些警告?

When using clojure.string, I receive the following warnings

WARNING: replace already refers to: #'clojure.core/replace in namespace: tutorial.regexp, being replaced by: #'clojure.string/replace
WARNING: reverse already refers to: #'clojure.core/reverse in namespace: tutorial.regexp, being replaced by: #'clojure.string/reverse

my clojure script is:

(ns play-with-it
  (:use [clojure.string]))

Is there any way to fix those warnings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在梵高的星空下 2025-01-13 14:08:35

是的,切换到

(ns play-with-it
  (:require [clojure.string :as string]))

,然后说例如

(string/replace ...)

调用 clojure.stringreplace 函数。

使用 :use,您可以将 clojure.string 中的所有变量直接引入到您的命名空间中,因为其中一些变量的名称与 clojure.core< 中的变量发生冲突。 /code>,您会收到警告。然后,您必须说 clojure.core/replace 才能获得通常简称为 replace 的内容。

名字的冲突是有意而为之的。 clojure.string 意味着需要使用这样的别名。 strstring 是最常选择的别名。

Yes, switch to

(ns play-with-it
  (:require [clojure.string :as string]))

and then say e.g.

(string/replace ...)

to call clojure.string's replace function.

With :use, you bring in all Vars from clojure.string directly into your namespace, and since some of those have names clashing with Vars in clojure.core, you get the warning. Then you'd have to say clojure.core/replace to get at what's usually simply called replace.

The clash of names is by design; clojure.string is meant to be required with an alias like this. str and string are the most frequently chosen aliases.

只是我以为 2025-01-13 14:08:35

除了 Michał 的回答之外,您还可以从 clojure.core 中排除变量:

user=> (ns foo)
nil
foo=> (defn map [])
WARNING: map already refers to: #'clojure.core/map in namespace: foo, being replaced by: #'foo/map
#'foo/map
foo=> (ns bar
        (:refer-clojure :exclude [map]))
nil
bar=> (defn map [])
#'bar/map

In addition to Michał's answer, you can exclude vars from clojure.core:

user=> (ns foo)
nil
foo=> (defn map [])
WARNING: map already refers to: #'clojure.core/map in namespace: foo, being replaced by: #'foo/map
#'foo/map
foo=> (ns bar
        (:refer-clojure :exclude [map]))
nil
bar=> (defn map [])
#'bar/map
深居我梦 2025-01-13 14:08:35

除了亚历克斯的答案之外,您还可以仅从给定的命名空间引用您想要的变量。

(ns foo.core
  (:use [clojure.string :only (replace-first)]))

这不会引发警告,因为 replace-first 不在 clojure.core 中。但是,如果执行以下操作,您仍然会收到警告:

(ns foo.core
  (:use [clojure.string :only (replace)]))

一般来说,人们似乎倾向于 (ns foo.bar (:require [foo.bar :as baz]))

In addition to Alex's answer you can also refer only the vars you want from a given namespace.

(ns foo.core
  (:use [clojure.string :only (replace-first)]))

This would not throw a warning since replace-first is not in clojure.core. However, you would still receive a warning if you did the following:

(ns foo.core
  (:use [clojure.string :only (replace)]))

In general it seems people are tending toward (ns foo.bar (:require [foo.bar :as baz])).

溇涏 2025-01-13 14:08:35

从 Clojure 1.4 开始,您可以使用 :require:refer 从命名空间引用您需要的各个函数:

(ns play-with-it
  (:require [clojure.string :refer [replace-first]]))

现在推荐使用 :use

假设您不需要 clojure.string/replace 或 clojure.string/reverse ,这也会删除警告。

请参阅这个问题此 JIRA 问题了解更多详细信息。

Since Clojure 1.4 you can refer the individual functions you need from a namespace using :require with a :refer:

(ns play-with-it
  (:require [clojure.string :refer [replace-first]]))

This is now recommended over :use.

Assuming you don't need the clojure.string/replace or clojure.string/reverse, that would also remove the warnings.

See this SO question and this JIRA issue for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文