Using clojure.string raises WARNINGS

When using clojure.string, I get 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:

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

Is there a way to fix these warnings?

+8
clojure
source share
4 answers

Yes, switch to

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

and then say for example

 (string/replace ...) 

to call the clojure.string replace function.

With :use you clojure.string all the Vars from clojure.string directly into your namespace, and since some of them have names that conflict with Vars in clojure.core , you get a warning. Then you have to say clojure.core/replace to get what is usually just called replace .

Collision of names by design; clojure.string means require d with such an alias. str and string are the most commonly used aliases.

+15
source share

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
+7
source share

In addition to the answer from Alex, you can also only refer to the desired vyres from a particular namespace.

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

This will not raise a warning since replace-first not in clojure.core . However, you will still receive a warning if you have done the following:

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

In general, it seems that people tend to (ns foo.bar (:require [foo.bar :as baz])) .

+4
source share

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

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

Now it is recommended for :use .

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

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

+1
source share

All Articles