Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using clojure.string causes WARNINGs

Tags:

clojure

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?

like image 454
viebel Avatar asked Feb 16 '12 23:02

viebel


3 Answers

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.

like image 96
Michał Marczyk Avatar answered Nov 18 '22 12:11

Michał Marczyk


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])).

like image 4
Devin Walters Avatar answered Nov 18 '22 12:11

Devin Walters


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
like image 7
Alex Taggart Avatar answered Nov 18 '22 13:11

Alex Taggart