"need" - add to contrib?

20 views
Skip to first unread message

Stuart Sierra

unread,
Apr 27, 2010, 11:22:19 AM4/27/10
to Clojure Dev
I posted this on IRC and Twitter yesterday, and response was
positive. If the list approves, I'll add it to Contrib. From there,
it can be considered for future versions of Clojure.
-SS

http://github.com/stuartsierra/need

need: a macro to load Clojure namespaces

The Need for need
=================

A lot of new Clojure users have trouble remembering the correct syntax
of use/require/refer.

The `need` macro is an attempt to simplify the process of loading
Clojure namespaces.

Unfortunately, `need` is not backwards-compatible with the existing
use/require/refer syntax. It would probably be too disruptive to
adopt this in Clojure right now, but it's an idea to think about for
the future.

I considered having `need` also replace `import` for Java classes, but
I think that loading Clojure namespaces and importing Java classes are
fundamentally different operations and should not be conflated.


Examples
--------

To load a namespace without referring any symbols:

(need clojure.contrib.io)

To load a namespace and refer two symbols:

(need [clojure.contrib.io reader writer])

To load a namespace and refer all symbols:

(need [clojure.contrib.io :all])

To load a namespace and refer all symbols except one:

(need [clojure.contrib.io :exclude [spit]])

To load a namespace and refer 3 symbols, renaming one of them. The
`:rename` map is like `{original-name new-name}`.

(need [clojure.contrib.io reader writer
:rename {spit put-file}])

To load a namespace and alias it in the current namespace:

(need [clojure.contrib.io :as io])


More
----

`need` does not support prefix lists because they are too confusing
when combined with vectors of symbols and make it hard to do
search-and-replace on namespace names.

`need*` is a function that behaves like `need` but evaluates its
arguments.

`need` and `need*` both accept the flags `:reload`, `:reload-all`, and
`:verbose`.

See additional documentation in the docstring of the `need` macro.

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

Phil Hagelberg

unread,
Apr 27, 2010, 12:12:00 PM4/27/10
to cloju...@googlegroups.com
On Tue, Apr 27, 2010 at 8:22 AM, Stuart Sierra
<the.stua...@gmail.com> wrote:
> I posted this on IRC and Twitter yesterday, and response was
> positive.  If the list approves, I'll add it to Contrib.  From there,
> it can be considered for future versions of Clojure.

I saw this and was intrigued, but I'm wondering how it meshes with the
overhaul of use that was raised here a while ago. Perhaps you should
coordinate with Steve Gilardi to avoid duplicating effort? I think
he's got some work towards this on a branch somewhere.

http://groups.google.com/group/clojure/browse_thread/thread/dbcfb0dc6699ec41/e8165da1ceebba9d

and

http://www.assembla.com/spaces/clojure/tickets/272-load-ns-require-use-overhaul

-Phil

Konrad Hinsen

unread,
Apr 27, 2010, 3:24:16 PM4/27/10
to cloju...@googlegroups.com
On 27.04.2010, at 17:22, Stuart Sierra wrote:

> The Need for need
> =================

Similar ideas have already been discussed on this list before.

> Examples
> --------
>
> To load a namespace without referring any symbols:
>
> (need clojure.contrib.io)
>
> To load a namespace and refer two symbols:
>
> (need [clojure.contrib.io reader writer])

Why the additional brackets in this and the following examples? I'd say just

(need clojure.contrib.io reader writer)

BTW, I have code for exactly this functionality here:

http://code.google.com/p/clj-nstools/source/browse/src/nstools/ns.clj#40

I'd happily donate it to contrib, or Clojure.

Konrad.

Mark Engelberg

unread,
Apr 27, 2010, 7:36:44 PM4/27/10
to clojure-dev
Is there any reason to prefer the name "need" to something already
established by another language, like say, "require"?

Seth Schroeder

unread,
Apr 28, 2010, 8:14:38 AM4/28/10
to Clojure Dev
Personally I like the idea of a distinct name. A lot of languages use
the terms import, include, require, use, &c. in slightly different
ways. I suppose I could also vote for "with" or "load".

Stuart Sierra

unread,
Apr 28, 2010, 1:09:23 PM4/28/10
to Clojure Dev
On Apr 27, 12:12 pm, Phil Hagelberg <p...@hagelb.org> wrote:
> I saw this and was intrigued, but I'm wondering how it meshes with the
> overhaul of use that was raised here a while ago. Perhaps you should
> coordinate with Steve Gilardi to avoid duplicating effort?

Looks like Steve G.'s "uses" and my "need" are very similar. The main
difference is he uses the pair ":refer-all true" where I just use
":all". Either way is fine by me.

We both eliminated prefix lists. This is good: it's easier to read &
understand, and better for external tools that parse the "ns" form.

Steve's version also disallows bare symbols. That is, every namespace
must be in a vector. I like that better too, for consistency and ease
of understanding.

Steve, are you there?

-SS

Stuart Sierra

unread,
Apr 28, 2010, 1:00:09 PM4/28/10
to Clojure Dev
On Apr 27, 7:36 pm, Mark Engelberg <mark.engelb...@gmail.com> wrote:
> Is there any reason to prefer the name "need" to something already
> established by another language, like say, "require"?

I just called it "need" so it wouldn't clash with any existing name.
I would prefer to replace "use" with the functionality implemented by
"need".

-SS

Stuart Sierra

unread,
Apr 28, 2010, 12:59:07 PM4/28/10
to Clojure Dev
On Apr 27, 3:24 pm, Konrad Hinsen <konrad.hin...@fastmail.net> wrote:
> > To load a namespace and refer two symbols:
>
> >    (need [clojure.contrib.io reader writer])
>
> Why the additional brackets in this and the following examples? I'd say just

Because you can load multiple namespaces in one 'need' expression;
each one goes in a vector.
-S

Stuart Sierra

unread,
Apr 28, 2010, 2:22:48 PM4/28/10
to Clojure Dev
On Apr 27, 11:22 am, Stuart Sierra <the.stuart.sie...@gmail.com>
wrote:
> I posted this on IRC and Twitter yesterday, and response was
> positive.  If the list approves, I'll add it to Contrib.  From there,
> it can be considered for future versions of Clojure.

I should clarify that I don't expect anyone to make use of "need" as
written. I just wanted to put it somewhere suitable for consideration
in an eventual overhaul of use/require/refer.

-SS

Dan Larkin

unread,
Apr 28, 2010, 11:21:24 PM4/28/10
to cloju...@googlegroups.com

On Apr 28, 2010, at 2:22 PM, Stuart Sierra wrote:

> On Apr 27, 11:22 am, Stuart Sierra <the.stuart.sie...@gmail.com>
> wrote:
>> I posted this on IRC and Twitter yesterday, and response was
>> positive. If the list approves, I'll add it to Contrib. From there,
>> it can be considered for future versions of Clojure.
>
> I should clarify that I don't expect anyone to make use of "need" as
> written. I just wanted to put it somewhere suitable for consideration
> in an eventual overhaul of use/require/refer.

IMHO contrib is not a suitable place. Like it or not, people are going to use what's there, and if you're just looking to stage your work somewhere I'd say keep it in its own repo on github, like you have it.

Just my 2¢

Stuart Sierra

unread,
Apr 29, 2010, 12:13:18 PM4/29/10
to Clojure Dev
On Apr 28, 11:21 pm, Dan Larkin <d...@danlarkin.org> wrote:
> IMHO contrib is not a suitable place. Like it or not, people are going to use what's there, and if you're just looking to stage your work somewhere I'd say keep it in its own repo on github, like you have it.

Yeah, I was coming 'round to that conclusion as well.
-SS
Reply all
Reply to author
Forward
0 new messages