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?
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 require
d with an alias like this. str
and string
are the most frequently chosen aliases.
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]))
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With