require namespace implicitly

91 views
Skip to first unread message

Mike Hinchey

unread,
Sep 26, 2010, 7:34:32 PM9/26/10
to cloju...@googlegroups.com
For quick scripts, I thought it would be nice to not have to call require or use to load a namespace, when it's easier to just name it (like you can a fully qualified java class).

I fixed the code to make this work.  It's a small change in Compiler that tries to call (require x) if the fully qualified classname is not found.  This gives priority to the java class, which protects backwards compatibility.

Would this be a welcome ticket and patch, or more discussion?  Is there any reason this shouldn't work?

For example: java -cp src/clj/:classes clojure.main -e "(clojure.set/union #{1} #{2})"

Currently on master, this produces: Exception in thread "main" java.lang.ClassNotFoundException: clojure.set
but this works: java -cp src/clj/:classes clojure.main -e "(require 'clojure.set) (clojure.set/union #{1} #{2})"

Obviously, (use) would make the code shorter, but my goal is to make it implicit.

-Mike

Paul Stadig

unread,
Sep 27, 2010, 8:33:09 PM9/27/10
to cloju...@googlegroups.com
I think you have a +1 from me. Though I'm not if there are any consequences I'm missing.

Paul

--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To post to this group, send email to cloju...@googlegroups.com.
To unsubscribe from this group, send email to clojure-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/clojure-dev?hl=en.

Stuart Halloway

unread,
Sep 28, 2010, 8:53:04 PM9/28/10
to cloju...@googlegroups.com
This scares me, both from a performance and semantics perspective:

* Do you want an extra if branch on every symbol in function position? If not, what are the rules, and how are they not confusing?

* Even not considering the performance, such a change would make code loading implicit and magical, with no lower level to escape to for anyone who didn't want it.

Stu

Dimitry Gashinsky

unread,
Sep 28, 2010, 9:19:31 PM9/28/10
to cloju...@googlegroups.com
-1

I do not understand why one need explicit naming on command line. But
this reads quite explicit to me.

java -jar clojure.jar -e "(use 'clojure.set) (union #{1} #{2})"

DiG

Paul Stadig

unread,
Sep 28, 2010, 10:15:24 PM9/28/10
to cloju...@googlegroups.com

Why would there have to be any more of a performance hit? Catch the class not found exception and try to load code. The JVM I already doing the checking.

If you fully qualify a Java class name it loads code. It doesn't seem a great semantic or syntactic leap to do the same for clojure namespaces, which are classes.

Or am I being too simplistic?

Mike Hinchey

unread,
Sep 28, 2010, 10:19:49 PM9/28/10
to cloju...@googlegroups.com
Paul, that's exactly right.  This just brings clojure namespaces the same feature we already have for java classes.

Another use case is when I'm writing code, and want some function from contrib or debug repl.  Can I paste one line (fully qualified) into my code, or do I have to require it somewhere else in the file, then add my one line.

-Mike

Paul Stadig

unread,
Sep 28, 2010, 10:23:11 PM9/28/10
to cloju...@googlegroups.com

I mean I'm not saying you should rely on this all over your production app, but for repl usage I can see the benefit.


> .
>>> For more options, visit this group at
> http://groups.google.com/group/clojure-dev?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
> "Clojure Dev" group.
>> To post to this group, send email to cloju...@googlegroups.com.
>> To unsubscribe from this group, send email to

Chouser

unread,
Sep 29, 2010, 9:01:28 AM9/29/10
to cloju...@googlegroups.com
On Tue, Sep 28, 2010 at 8:53 PM, Stuart Halloway
<stuart....@gmail.com> wrote:
> This scares me, both from a performance and semantics perspective:
>
> * Do you want an extra if branch on every symbol in function position? If not, what are the rules, and how are they not confusing?

I think this would actually have to happen at compile time, as with
all var name resolution. When the var is resolved by the compiler, it
would find no such namespace or class and attempt to 'require' it. If
that succeeds, try once more to resolve the var in that namespace --
if either that or the 'require' itself fails, give up. I'm not sure
if you'd have to do something different for AOT, like emit code to do
that same require in the namespace's init. In short, no runtime
performance implication at all.

> * Even not considering the performance, such a change would make code loading implicit and magical, with no lower level to escape to for anyone who didn't want it.

As others have mentioned, I think that ship already sailed. If I say
some.old.KlassName/FOO to refer to a static Java member, I can at that
moment trigger all kinds of class init side-effect depending on the
specific contents of my classpath, very much like as is being proposed
for namespaces.

--Chouser
http://joyofclojure.com/

Laurent PETIT

unread,
Sep 29, 2010, 9:38:06 AM9/29/10
to cloju...@googlegroups.com
I've also thought about this possible feature more than once in the past. 
Of course by allowing this, it would be harder to statically spot all the deps of namespace from its (ns) decl. But aren't there already many reasons for this static analysis dep relying only on (ns) to not work as expected ( standalon (import) (use) (require) for example, not to mention the fact that *when* the namespace has already be loaded by another namespace, using the fully qualified form will work even if there has been no explicit (require) from the current namespace ...

2010/9/29 Chouser <cho...@gmail.com>

Chas Emerick

unread,
Oct 15, 2010, 2:58:10 PM10/15/10
to Clojure Dev
I actually just hit a situation where this would be particularly
helpful: using clutch's view server for CouchDB, one needs to
(require ...) and then (resolve ...) in order to get around not
(currently) having a way to require a namespace before referring to
one of its definitions.

+1

Mike, is there a ticket for this in assembla?

- Chas

Mike Hinchey

unread,
Oct 16, 2010, 3:13:38 AM10/16/10
to cloju...@googlegroups.com
I'll create one and make a patch and see what happens.

-Mike

Nicolas Buduroi

unread,
Oct 16, 2010, 1:22:21 PM10/16/10
to Clojure Dev
+1 from me too. Lots of possible usage in code for temporary stuff,
and also great idea for the repl.

On Sep 26, 7:34 pm, Mike Hinchey <hinche...@gmail.com> wrote:

Stuart Sierra

unread,
Oct 16, 2010, 5:42:56 PM10/16/10
to Clojure Dev
Scares me too. -1

S

Chas Emerick

unread,
Oct 16, 2010, 7:21:14 PM10/16/10
to cloju...@googlegroups.com
I think Paul's points made upthread are good; how is this any
different than the implicit access and initialization of fully-named
Java classes, with (AFAICT) all perf considerations confined to
compile time?

- Chas

Steve Miner

unread,
Oct 17, 2010, 2:50:56 PM10/17/10
to cloju...@googlegroups.com
When I first played around with Clojure, I had that exact problem of getting clojure.set/union to work in the REPL. Eventually, I found (require 'clojure.set) but it was a bit frustrating as a day 1 experience. It might help if the error message said "(require 'clojure.set) is necessary before accessing the clojure.set library".

I might call the proposed feature "auto-require". It could be a property of your namespace, something like the :refer-clojure reference option for the ns macro.

For example, to allow auto-require to work for any lib, except clojure.dangerous, you could write:

(ns foo.bar
(:auto-require :exclude [clojure.dangerous]))

If the programmer is concerned about too much magic, he can turn off the feature like:

(ns foo.bar
(:auto-require :include []))

In which case, no lib can be auto-required, and you would get today's behavior.

Personally, I would prefer that when no :auto-require reference is given, the effect would be: (:auto-require :exclude []). That is, implicit namespace recognition works for any lib. But if the new feature is not universally accepted, you could make the default be (:auto-require :include []).

In any case, I think it would definitely be a nice feature for the user namespace as it would make it easier to get started.

Steve Miner

Christophe Grand

unread,
Oct 17, 2010, 4:17:42 PM10/17/10
to clojure-dev
+1 for me

--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To post to this group, send email to cloju...@googlegroups.com.
To unsubscribe from this group, send email to clojure-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/clojure-dev?hl=en.




--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

Phil Hagelberg

unread,
Oct 17, 2010, 5:36:29 PM10/17/10
to cloju...@googlegroups.com
On Sun, Oct 17, 2010 at 1:17 PM, Christophe Grand <chris...@cgrand.net> wrote:
> +1 for me

The fact is it's quite easy to have a namespace A that relies on
another namespace B having being required without explicitly listing B
in A's ns form; you don't notice the fact that the explicit require is
missing because C requires B and you've always had C loaded before
loading A. I'm sure this has happened a lot already with clojure.set
since clojure.set has been required implicitly in the past. So it's
easy to have namespaces that only work by accident, and this proposal
makes that problem go away without introducing any new actual problems
beyond "it makes me feel nervous".

+1

-Phil

Laurent PETIT

unread,
Oct 17, 2010, 5:44:14 PM10/17/10
to cloju...@googlegroups.com


2010/10/17 Phil Hagelberg <ph...@hagelb.org>

That's what I already said some more posts above (though you explained it better), and for me too, it's a

+1
 

-Phil

Stephen C. Gilardi

unread,
Oct 17, 2010, 6:39:38 PM10/17/10
to cloju...@googlegroups.com
inc

Cosmin Stejerean

unread,
Oct 18, 2010, 2:30:15 PM10/18/10
to cloju...@googlegroups.com
+1

Shall we move this to a ticket and a patch?

- Cosmin

Mike Hinchey

unread,
Oct 19, 2010, 1:30:55 AM10/19/10
to cloju...@googlegroups.com
Ticket and patch are up so anyone can review.

-Mike

Reply all
Reply to author
Forward
0 new messages