Basic question: what's the difference between "require as" and "use as"?

199 views
Skip to first unread message

Ritchie Cai

unread,
Jun 22, 2015, 4:50:55 PM6/22/15
to clo...@googlegroups.com
For example:
(:require [clojure.java.io :as io]) 
vs
(:use [clojure.java.io :as io])

In both cases, we can end up using it with "(io/<function name> ... )". Is there a difference at all?

Thanks
Ritchie


Sean Corfield

unread,
Jun 22, 2015, 5:15:57 PM6/22/15
to clo...@googlegroups.com
Yes, (:use [clojure.java.io :as io]) is equivalent to (:require [clojure.java.io :as io :refer :all]) so it refers in all of the symbols from that namespace directly into your namespace (i.e., they can be referenced even without the io/ prefix).

Sean Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)



Ritchie Cai

unread,
Jun 22, 2015, 5:22:25 PM6/22/15
to clo...@googlegroups.com
You mean (:use [clojure.java.io]) is equivalent to (:require [clojure.java.io :as io :refer :all])?

Ritchie
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ned76MpB-W0/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sean Corfield

unread,
Jun 22, 2015, 5:32:55 PM6/22/15
to clo...@googlegroups.com
On Jun 22, 2015, at 2:22 PM, Ritchie Cai <ritch...@gmail.com> wrote:
> You mean (:use [clojure.java.io]) is equivalent to (:require [clojure.java.io :as io :refer :all])?

Not quite, (:use [clojure.java.io]) is equivalent to (:require [clojure.java.io :refer :all])

Ritchie Cai

unread,
Jun 22, 2015, 5:39:30 PM6/22/15
to clo...@googlegroups.com
Ok, so require without :refer will default to :refer :all?

Sean Corfield

unread,
Jun 22, 2015, 5:52:42 PM6/22/15
to clo...@googlegroups.com
On Jun 22, 2015, at 2:39 PM, Ritchie Cai <ritch...@gmail.com> wrote:
> Ok, so require without :refer will default to :refer :all?

No.

>> (:use [clojure.java.io]) is equivalent to (:require [clojure.java.io :refer :all])

There’s no :as here. :use is like :require :refer :all.

>> (:use [clojure.java.io :as io]) is equivalent to (:require [clojure.java.io :as io :refer :all])

There’s an :as in both here. :use :as is like :require :as :refer :all

Both :use and :refer :all (in :require) are discouraged because they pollute your namespace with every symbol from the used/required namespace.

There’s not much point in specifying an alias (with :as) for :use or :require :refer :all since those bring in every symbol directly anyway.

Ritchie Cai

unread,
Jun 22, 2015, 6:07:25 PM6/22/15
to clo...@googlegroups.com
Ah, I see. I thought “use :as” will not intern all the symbols into current namespace, apparently that’s not the case.
Thanks for clearing this up.

Ritchie

Gary Verhaegen

unread,
Jun 23, 2015, 7:37:30 AM6/23/15
to clo...@googlegroups.com
Unless you have a very compelling reason, just don't use use. It's mostly a historical accident that's kept there for backwards compatibility.

And try to avoid :refer :all if possible. There are legitimate use-cases for it, but outside the REPL they are pretty rare.
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Ritchie Cai

unread,
Jun 23, 2015, 10:40:25 AM6/23/15
to clo...@googlegroups.com
I always have (:use clojure.core) in a new namespace. Is that necessary or is clojure.core is automatically interned when a new namespace is created?

Fluid Dynamics

unread,
Jun 23, 2015, 12:02:22 PM6/23/15
to clo...@googlegroups.com
On Tuesday, June 23, 2015 at 10:40:25 AM UTC-4, Ritchie Cai wrote:
I always have (:use clojure.core) in a new namespace. Is that necessary or is clojure.core is automatically interned when a new namespace is created?

"It depends." Yes if you use (ns foo) ..., but not apparently if you use (in-ns 'foo) which might be done in certain metaprogramming situations, or during repl experimentation.
 

Ritchie Cai

unread,
Jun 23, 2015, 12:20:52 PM6/23/15
to clo...@googlegroups.com
I see. Thanks.
Reply all
Reply to author
Forward
0 new messages