ns/use/require/import inconsistent an confusing

205 views
Skip to first unread message

Terje Dahl

unread,
Nov 30, 2011, 4:58:05 AM11/30/11
to Clojure
Consider the following:

(ns my.namespace.here)

(use 'my.namespace.here :reload)

(require '[my.namespace.here :as something])

(import [my.package MyClass1 MyClass2])


1. Why (at least!) 4 different ways of noting namspaces/domains: With
or without the escape-mark (') and with or without square
brackets([])?
Does it have to do with macros vs. functions etc? Must it be so?
Surely it could be better!


2. Is it stricly necessary to have both "use", "require", and "import"
- as well as a host of keywords for the different ones?
Could they perhaps be combined to 2 - or even one?

Dare I say Python seems to have a nice, simple version of a combined
use/require sollution:
import my.module
import my.module as m1
from my.module import MyClass1, MyClass2
etc.
Cojures "require" allready allows ":as" and Clojures "use" has ":only"
followed by a list.

And would it be technically possible to:
I. Combine a "use"/"require" combined sollution with the "import"-
function?
II. Also have a ":reload" and ":reload-all" for Java classes?


Is all this a case of "that's what me had time for for version 1, but
we are working on something better."?
Or is it technically impossible to improve on or abstract away any of
this?


The many choices and inconsistencies in the different import-
statements is complicated and confusing.
And as this is the first thing you have to figure out as a new-to-
Clojure programmer (novice or professional) to start programming in
Clojure, it certainly doen't make for a very good first impression!

j1n3l0

unread,
Nov 30, 2011, 6:06:04 AM11/30/11
to clo...@googlegroups.com
Also in a REPL you can do the following:

user> (use 'example.core)
nil
user>

But in a source file you have to do:

(ns stuff.core
  (:use 'example.core))

I think I've seen the reason for this difference once before but can someone more learned please explain :)

Thanks

Nelo

Ps: this exact discussion came up in the London Clojure Dojo last night :)


jongwon.choi

unread,
Nov 30, 2011, 7:27:34 AM11/30/11
to clo...@googlegroups.com
Even though I've written only one toy web framework in Clojure[1], (I think) I know the difference.

Because there is documentation for use and ns, I'll add my own only :)

I interpret "(use 'ns)" as "Use 'ns namespace within *current* namespace - from your example, 'user'. Usually for REPL.

(ns ...) is a definition of namespace. You can define any namespace in any file, I guess (I didn't try that but it seems it's the right thing to me).

I interpret "(ns ns1 (:use 'ns2)" as "Define ns1 namespace and from ns1 namespace use stuff in ns2 namespace. Usually for files.

I bet ns is a macro and use 'use' - that's what I'll do. You should try macroexpand those things to learn how it works and think such a way. Once you have this ability, when you see something new and read doc or usage of it, you can imagine if it is function or macro, and (for macro) how underlying macroexpansion would look like, etc.

Stuart Sierra

unread,
Nov 30, 2011, 10:01:36 AM11/30/11
to Clojure
This has been heavily discussed, and some work is underway to mitigate
it.

See http://dev.clojure.org/display/design/Loading%2C+Compiling%2C+and+Namespaces

-S

Reply all
Reply to author
Forward
0 new messages