Only one :require et al in `ns` macro?

已查看 329 次
跳至第一个未读帖子

Sean Corfield

未读,
2012年3月6日 13:11:062012/3/6
收件人 cloju...@googlegroups.com
This issue came up on the main Clojure list so I wanted to bring it up
on -dev to get more specific implementation feedback:

Stuart Sierra said:

"Yes, it is incorrect, in both Clojure and ClojureScript, to repeat
the (:require ...) or (:use ...) forms in an `ns` declaration."

Based on code from Aaron:
>
> (ns my-namespace
>  (:require [lib1 :as l1])
>  (:require [lib2 :as l2]))

This doesn't compile correctly for ClojureScript:

> The compiler misses the first require.

I've run into this as well but was able to reorganize my code to avoid
the issue so I didn't spend any time chasing it down.

Stuart's response really surprised me because:
a) multiple :require's work fine in Clojure (JVM)
b) lots of examples / blogs etc out there show multiple :require's -
including the 8th light blog about "namespaces, use and require" which
everyone gets referred to
c) lots of existing code uses multiple :require's etc

If multiple :require's is really incorrect, shouldn't the `ns` macro
output at least a warning? (in Clojure JVM, CLR and Script) And all
the documentation out there needs to be updated...
If multiple :require's is supposed to work, then this is a bug in ClojureScript.

What's the definitive position here?

FWIW, Clojure in Action has at least one example with multiple
:require's and Joy of Clojure has at least one example with multiple
:use's so the written / printed word also seems to think this should
work. Programming Clojure seems to be very careful to have at most one
of each. I don't have a copy of Practical Clojure on hand to check
that.
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

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

Cosmin Stejerean

未读,
2012年3月6日 13:19:292012/3/6
收件人 cloju...@googlegroups.com
It seems to me that multiple require and use should work and the ns
macro should take care of it. Perhaps this wasn't intended, but now
seems a little too late to tell everyone that they shouldn't do it
this way.


- Cosmin

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

--
Cosmin Stejerean
http://offbytwo.com

Colin Jones

未读,
2012年3月6日 13:41:222012/3/6
收件人 cloju...@googlegroups.com
On Tue, Mar 6, 2012 at 12:11 PM, Sean Corfield <seanco...@gmail.com> wrote:
> This issue came up on the main Clojure list so I wanted to bring it up
> on -dev to get more specific implementation feedback:
>
> Stuart Sierra said:
>
> "Yes, it is incorrect, in both Clojure and ClojureScript, to repeat
> the (:require ...) or (:use ...) forms in an `ns` declaration."
>
> Based on code from Aaron:
>>
>> (ns my-namespace
>>  (:require [lib1 :as l1])
>>  (:require [lib2 :as l2]))
>
> This doesn't compile correctly for ClojureScript:
>
>> The compiler misses the first require.
>
> I've run into this as well but was able to reorganize my code to avoid
> the issue so I didn't spend any time chasing it down.
>
> Stuart's response really surprised me because:
> a) multiple :require's work fine in Clojure (JVM)
> b) lots of examples / blogs etc out there show multiple :require's -
> including the 8th light blog about "namespaces, use and require" which
> everyone gets referred to

To this specific point, if the consensus is that this shouldn't be
allowed in Clojure, then I'm happy to update my blog post to reflect
that. In such a case, perhaps the docstring could be clarified a bit
to make that more obvious? It wouldn't be strictly wrong as-is, but
could be more helpful.

> c) lots of existing code uses multiple :require's etc
>
> If multiple :require's is really incorrect, shouldn't the `ns` macro
> output at least a warning? (in Clojure JVM, CLR and Script) And all
> the documentation out there needs to be updated...
> If multiple :require's is supposed to work, then this is a bug in ClojureScript.
>
> What's the definitive position here?
>
> FWIW, Clojure in Action has at least one example with multiple
> :require's and Joy of Clojure has at least one example with multiple
> :use's so the written / printed word also seems to think this should
> work. Programming Clojure seems to be very careful to have at most one
> of each. I don't have a copy of Practical Clojure on hand to check
> that.

I don't see any examples of it in Practical Clojure.


> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View -- http://corfield.org/
> World Singles, LLC. -- http://worldsingles.com/
>
> "Perfection is the enemy of the good."
> -- Gustave Flaubert, French realist novelist (1821-1880)
>

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

--
Colin Jones
@trptcolin

Alan Malloy

未读,
2012年3月6日 14:22:202012/3/6
收件人 Clojure Dev
For what it's worth, I think ns supports multiple :require clauses
intentionally. If it intended to support only one, it would be simpler
to design its input to take a map, like:

(ns my.ns
{:require ...
:use ...})

Then supplying a clause twice would cause a reader exception. The
awkward use of alists for grouping implies to me that it offers some
advantage that a map wouldn't have, and the most obvious seems to be
allowing repeated clauses.
> World Singles, LLC. --http://worldsingles.com/

Allen Rohner

未读,
2012年3月6日 14:32:222012/3/6
收件人 Clojure Dev

> > Stuart Sierra said:
>
> > "Yes, it is incorrect, in both Clojure and ClojureScript, to repeat
> > the (:require ...) or (:use ...) forms in an `ns` declaration."
>

I'd like to point out this is a huge change. The original discussions
of the ns macro, and the current official docs say nothing of this
limitation. IMO, it's completely unreasonable to make this breaking
change without very good reason.

Stuart Sierra

未读,
2012年3月6日 15:31:452012/3/6
收件人 cloju...@googlegroups.com

"Yes, it is incorrect, in both Clojure and ClojureScript, to repeat
the (:require ...) or (:use ...) forms in an `ns` declaration."


This is only my opinion, and (apparently) not widely shared. But I stand behind it. The fact that it works in Clojure/JVM is an implementation detail, like everything else about the `ns` macro.

But if everyone already does it and it's possible to support it in ClojureScript, by all means submit a patch to make it work.

-S

Cosmin Stejerean

未读,
2012年3月6日 15:48:252012/3/6
收件人 cloju...@googlegroups.com
On Tue, Mar 6, 2012 at 12:31 PM, Stuart Sierra
<the.stua...@gmail.com> wrote:
> is an implementation detail, like everything else about the `ns` macro.

I'm not sure I understand what you mean by that. What else about the
ns macro is an implementation detail that should not be relied upon?

Stuart Sierra

未读,
2012年3月6日 16:29:012012/3/6
收件人 cloju...@googlegroups.com
On Tuesday, March 6, 2012 3:48:25 PM UTC-5, cosmin wrote:

I'm not sure I understand what you mean by that. What else about the
ns macro is an implementation detail that should not be relied upon?


Everything. The `ns` macro is poorly specified. It allows far too many variations, and too many people have come to expect that certain forms will always work. Like the fact that you can use vectors in a lot of places where lists are specified. Or the fact that :use/:require will sometimes work as symbols, not keywords. And all that nonsense around prefix lists.
-S 

Sean Corfield

未读,
2012年3月6日 18:26:442012/3/6
收件人 cloju...@googlegroups.com
On Tue, Mar 6, 2012 at 1:29 PM, Stuart Sierra
<the.stua...@gmail.com> wrote:
> Everything. The `ns` macro is poorly specified. It allows far too many
> variations, and too many people have come to expect that certain forms will
> always work. Like the fact that you can use vectors in a lot of places where
> lists are specified. Or the fact that :use/:require will sometimes work as
> symbols, not keywords. And all that nonsense around prefix lists.

I'll agree with all of that. (ns foo (use blah)) is a good example of
something that works but probably shouldn't.

I get the impression that (:import ..) requires lists but (:require
..) and (:use ..) accept vectors - and you're saying that should be
cleaned up? (given the prevalence of vectors for :require/:use,
:import should accept vectors, yes?)

Perhaps Clojure/core could write a definitive spec of what `ns` should
too and we can take a look at the delta between what's supposed to
work and what actually works (and what is commonly expected to work)?

The status quo is certainly a problem and the longer we leave it, the
harder it will be to restrict `ns` to just what is intended to work vs
what works by accident - and this will become increasingly important
as alternative Clojure implementations appear / grow (CLR, Script,
Python...).

Stuart Sierra

未读,
2012年3月7日 16:44:512012/3/7
收件人 cloju...@googlegroups.com
The short version, in my highly opinionated view:

- Each clause must be a list
- Each clause must begin with a keyword
- Only one instance of each type of clause (:require, :use, etc.)
- No prefix lists except in :import
   - They must be lists, not vectors
   - No bare symbols in :import
- :use is deprecated as of Clojure 1.4

Example:

(ns
  (:require
    foo.bar
    [foo.bar.baz :as baz]
    [foo.bar.quux :refer [a b c]])
  (:use
    baz.bar
    [baz.bar :only [a b c]])
  (:import
    (java.util List Set Map)
    (java.util.concurrent AtomicLong)))

Aaron Cohen

未读,
2012年3月7日 16:57:472012/3/7
收件人 cloju...@googlegroups.com
On Wed, Mar 7, 2012 at 4:44 PM, Stuart Sierra
<the.stua...@gmail.com> wrote:
> The short version, in my highly opinionated view:
>
> - No prefix lists except in :import
>    - They must be lists, not vectors

I completely disagree on this one, personally.

1) This would make the interop form "more powerful" than the native
one. Why do you want me to repeat "clojure" over and over here?
https://github.com/remleduff/CinC/blob/master/src/clojure/java/compiler.clj#L5

2) Lists should be used for things that look like invocation. (:use)
is appropriate, (java.util List) seems wrong, nothing is being invoked
and "should" be quoted in normal clojure. Vectors are much more
appropriate in my highly opinionated view. :)


>    - No bare symbols in :import

This just seems like a gratuitous difference between :use/:require and
:import, though I do almost always end up using prefix lists for
:import.

Kevin Downey

未读,
2012年3月7日 17:08:232012/3/7
收件人 cloju...@googlegroups.com

stuart's example here is incorrect, correct it looks like:

https://gist.github.com/1996557

> --
> You received this message because you are subscribed to the Google Groups
> "Clojure Dev" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/clojure-dev/-/apV9oNtMX3oJ.


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

--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

Stuart Sierra

未读,
2012年3月7日 17:12:002012/3/7
收件人 cloju...@googlegroups.com

stuart's example here is incorrect, correct it looks like:



It is not incorrect, but it is my personal opinion. Some parts (bare symbols in :use) are not currently supported by ClojureScript.
-S

Stuart Sierra

未读,
2012年3月7日 17:12:582012/3/7
收件人 cloju...@googlegroups.com
This is why I don't write specs.
-S

Kevin Downey

未读,
2012年3月7日 17:13:302012/3/7
收件人 cloju...@googlegroups.com

well, I don't write clojurescript, I write clojure were bare symbols
are supported, but they are still incorrect


> --
> You received this message because you are subscribed to the Google Groups
> "Clojure Dev" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/clojure-dev/-/uCye7T83BW8J.

Philip Potter

未读,
2012年3月8日 03:53:062012/3/8
收件人 cloju...@googlegroups.com
On 7 March 2012 21:44, Stuart Sierra <the.stua...@gmail.com> wrote:
> The short version, in my highly opinionated view:
>
> - :use is deprecated as of Clojure 1.4

Wait, is this one still your opinion or is it fact? :use is
deprecated? I'd be very dismayed if that were the case. It's a bad
practice in a lot of coding environments, sure, but it's indispensible
when live-coding overtone.

Phil

Stuart Sierra

未读,
2012年3月8日 13:17:132012/3/8
收件人 cloju...@googlegroups.com
All opinion. Every word. Not an ounce of truth to it.
-S

pmbauer

未读,
2012年3月8日 15:15:272012/3/8
收件人 cloju...@googlegroups.com
Eh? Pretty durned decent opinion.
+1

Sean Corfield

未读,
2012年4月16日 22:42:302012/4/16
收件人 cloju...@googlegroups.com
Just want to follow up on this and say: having my code base on 1.4.0
now and being able to have a single :require with appropriate :as or
:refer entries is bliss!
回复全部
回复作者
转发
0 个新帖子