2012-02-16 80 views
8

当使用clojure.string,我收到以下警告使用clojure.string导致警告

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 

我Clojure的脚本是:

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

有没有什么办法来解决这些警告?

回答

15

是,切换到

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

然后说例如

(string/replace ...) 

调用clojure.stringreplace功能。

随着:use,你从clojure.string直接进入你的名字空间的所有瓦尔斯,因为其中一些名称与瓦尔斯冲突在clojure.core,你会得到警告。然后你必须说clojure.core/replace得到什么通常简称为replace

名称的冲突是由设计; clojure.string的含义是required,带有这样的别名。 strstring是最常选择的别名。

+0

谢谢。是否有可能从'use'文档中看到这个问题?'像'require'一样,还可以使用'clojure.core/refer'引用每个lib的命名空间。“这句话是什么意思? – viebel 2012-02-16 23:22:41

+1

这意味着''use'完全符合'require'的要求,然后再询问'refer'来在当前命名空间中为名字空间被'use'd导出的东西创建映射。 (我所称的*将[use'd命名空间]中的所有Vars直接引入上面的命名空间*中。)Cf. '(doc参考)'。 – 2012-02-16 23:29:22

+0

Clojure的快乐解释这些也很好。 – Bill 2012-02-17 00:19:22

7

除了Michal的回答,您可以从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 
4

除了亚历克斯的答案,你也可以指只从一个给定的命名空间所需的增值经销商。

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

因为replace-firstclojure.core这不会抛出一个警告。但是,你仍然会得到一个警告,如果你做了以下内容:

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

一般人们似乎朝着(ns foo.bar (:require [foo.bar :as baz]))趋向。

1

由于Clojure的1.4,你可以使用:require:refer命名空间是指你所需要的各个功能:

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

这现在更推荐:use

假设您不需要clojure.string/replaceclojure.string/reverse,那也将删除警告。

请参阅this SO questionthis JIRA issue了解更多详情。