I love Clojure. But: My number one complaint about it is that the compiler won't allow cyclic namespace dependencies. I am not a compiler-writer, but as far as I can tell the only reason for this restriction is ... philosophical. It forces bad code organization, and it wastes time. Those who think that cyclic code is an evil are free to encourage its avoidance. I'm happy to avoid it! But sometimes I can't. I absolutely hate the cyclic namespace restriction. Drives me crazy when I am forced to encounter it.
My latest case is that I'm trying to debug a weird Java interop problem--something that works in one app I've written, in which I ended up putting most of the application into one file, but doesn't work in the one I'm working on now. And I had finally come up with a clever way of avoiding sticking most of the code into one namespace. I'd worked around the conflict between Clojure's requirements and the requirements of the Java library I'm using. But now there's this one problem, and I want to try an experiment to see whether it fixes the problem. It looks like I'm going to have to pack all of the code into one file and rewrite everything that that necessitates just for this purpose. Aghh.
I have sometimes ranted a bit in this group, and in the end some of my rants were unreasonable. I don't think this is unreasonable. The cyclic dependency restriction is absurd. I'm not trying to start a flame ware. I feel strongly about this, and I'm frustrated.
What I want: I'm not trying to make everyone sit around tapping their fingers while code to goes through multi-pass compliations. All I'm asking for is a compiler option that will allow cyclic dependencies when requested.
I'm pretty sure that the way I've put things above illustrates some ignorance on my part, but still ... how hard can this be? It's a reasonable thing to ask of a sophisticated compiler.
On Friday, December 30, 2016 at 10:13:49 AM UTC-6, Mars0i wrote:I love Clojure. But: My number one complaint about it is that the compiler won't allow cyclic namespace dependencies. I am not a compiler-writer, but as far as I can tell the only reason for this restriction is ... philosophical. It forces bad code organization, and it wastes time. Those who think that cyclic code is an evil are free to encourage its avoidance. I'm happy to avoid it! But sometimes I can't. I absolutely hate the cyclic namespace restriction. Drives me crazy when I am forced to encounter it.
First of all, there is no such restriction. So there is no reason for it, philosophical or otherwise.
It is true that if you declare all your dependencies in ns forms, you will get an exception with cyclic namespaces. That's a good thing -- the result probably wouldn't be what you want anyway. However, there's no requirement that all dependencies be declared using ns -- you can use require at any time.
What is required is that all the vars a form uses exist when it is compiled. The reason for this is purely technical -- resolving symbols to vars at compile time means less work (more efficiency) at run time.
My latest case is that I'm trying to debug a weird Java interop problem--something that works in one app I've written, in which I ended up putting most of the application into one file, but doesn't work in the one I'm working on now. And I had finally come up with a clever way of avoiding sticking most of the code into one namespace. I'd worked around the conflict between Clojure's requirements and the requirements of the Java library I'm using. But now there's this one problem, and I want to try an experiment to see whether it fixes the problem. It looks like I'm going to have to pack all of the code into one file and rewrite everything that that necessitates just for this purpose. Aghh.
Perhaps if you provided a coherent description the specifics of your problem, someone would be able to suggest a satisfactory solution.
I have sometimes ranted a bit in this group, and in the end some of my rants were unreasonable. I don't think this is unreasonable. The cyclic dependency restriction is absurd. I'm not trying to start a flame ware. I feel strongly about this, and I'm frustrated.
What would be reasonable is asking questions about things until you understand them well enough to be in a position to make an informed opinion.
In this case:
- Clojure does allow namespaces to refer to each other (just maybe not how you've tried).
- There are plenty of technical considerations when it comes to mixing dynamic languages and cyclic references. Cyclic namespaces are a subset of this problem. So describing restrictions on cyclic namespaces (whether real or imagined) as "absurd" is absurd.
A rant is just a rant, regardless of whether a problem actually exists. If you feel strongly about something and only rant about it, don't be surprised when you feel frustrated.
Frustration can be relieved by solutions.
If you don't understand the problem enough to present a solution, ask questions until you do. Don't expect others to do all the work -- especially after ranting about it.
What I want: I'm not trying to make everyone sit around tapping their fingers while code to goes through multi-pass compliations. All I'm asking for is a compiler option that will allow cyclic dependencies when requested.
I don't think multi-pass compilation would play well with things like macros or REPL-based development, so I doubt we'll have to worry about that.
I'm pretty sure that the way I've put things above illustrates some ignorance on my part, but still ... how hard can this be? It's a reasonable thing to ask of a sophisticated compiler.
Questions like "How hard can this be?" are not only unconstructive, but they also belie your sincerity in resolving your self-claimed ignorance.
The issue is not single-pass vs multi-pass. It is instead, what constitutes a compilation unit, i.e., a pass over what?Clojure, like many Lisps before it, does not have a strong notion of a compilation unit. Lisps were designed to receive a set of interactions/forms via a REPL, not to compile files/modules/programs etc. This means you can build up a Lisp program interactively in very small pieces, switching between namespaces as you go, etc. It is a very valuable part of the Lisp programming experience. It implies that you can stream fragments of Lisp programs as small as a single form over sockets, and have them be compiled and evaluated as they arrive. It implies that you can define a macro and immediately have the compiler incorporate it in the compilation of the next form, or evaluate some small section of an otherwise broken file. Etc, etc. That "joke from the 1980's" still has legs, and can enable things large-unit/multi-unit compilers cannot. FWIW, Clojure's compiler is two-pass, but the units are tiny (top-level forms).
What Yegge is really asking for is multi-unit (and larger unit) compilation for circular reference, whereby one unit can refer to another, and vice versa, and the compilation of both units will leave hanging some references that can only be resolved after consideration of the other, and tying things together in a subsequent 'pass'. What would constitute such a unit in Clojure? Should Clojure start requiring files and defining semantics for them? (it does not now)
Forward reference need not require multi-pass nor compilation units. Common Lisp allows references to undeclared and undefined things, and generates runtime errors should they not be defined by then. Clojure could have taken the same approach. The tradeoffs with that are as follows:
1) less help at compilation time 2) interning clashes
While #1 is arguably the fundamental dynamic language tradeoff, there is no doubt that this checking is convenient and useful. Clojure supports 'declare' so you are not forced to define your functions in any particular order.
#2 is the devil in the details. Clojure, like Common Lisp, is designed to be compiled, and does not in general look things up by name at runtime. (You can of course design fast languages that look things up, as do good Smalltalk implementations, but remember these languages focus on dealing with dictionary-carrying objects, Lisps do not). So, both Clojure and CL reify names into things whose addresses can be bound in the compiled code (symbols for CL, vars for Clojure). These reified things are 'interned', such that any reference to the same name refers to the same object, and thus compilation can proceed referring to things whose values are not yet defined.
But, what should happen here, when the compiler has never before seen bar?
or in CL:(defn foo [] (bar))
CL happily compiles it, and if bar is never defined, a runtime error will occur. Ok, but, what reified thing (symbol) did it use for bar during compilation? The symbol it interned when the form was read. So, what happens when you get the runtime error and realize that bar is defined in another package you forgot to import. You try to import other-package and, BAM!, another error - conflict, other-package:bar conflicts with read-in-package:bar. Then you go learn about uninterning.(defun foo () (bar))
In Clojure, the form doesn't compile, you get a message, and no var is interned for bar. You require other-namespace and continue.
I vastly prefer this experience, and so made these tradeoffs. Many other benefits came about from using a non-interning reader, and interning only on definition/declaration. I'm not inclined to give them up, nor the benefits mentioned earlier, in order to support circular reference.
Rich
--
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+unsubscribe@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 the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
So the layout looks like this:Interfaces.clj|------------------------------------------------| |ImplementationA Implementation B| |------------------------------------------------|Orchestration (Setup) Namespace
--
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+unsubscribe@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 the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I can see that, and even spec has this use case. In Spec it's solved by having both A and B in one namespace and using declare to forward-declare the constructors (or defns in this case).So I guess the way I see it the tradeoff is a declare and all-in-one-namespace vs a massive complexity addition to the compiler and the redefinition of compilation units. The declare method seems like the cleaner route.
On Fri, Dec 30, 2016 at 4:55 PM, Timothy Baldridge <tbald...@gmail.com> wrote:I wonder whether there could be something like an `external-declare` that would satisfy Rich's concerns about knowing how to intern unencountered vars, while allowing cyclical references when needed.
The macros defined in ‘scarlett.core’ here: https://github.com/scgilardi/scarlett are an experiment along those lines.
scarlett
Provides macros to declare vars in namespaces other than *ns*
To be used sparingly to overcome otherwise cyclic namespace dependencies
Usage
(ns-declare my-module startup shutdown)
declares my-module/startup and my-module/shutdown
(declare+ module-a/startup module-b/startup)
declares module-a/startup and module-b/startup
They work by using “declare” after switching to the target namespace using “in-ns” (and switching back at the end). They have to be issued at the “top level” (as is highly recommended for ns, declare, def, etc.) to be effective and they contain code to notify with an exception if they are used at other than the top level. (The exception is preferable to silent failure or a confusing error message.)
The macros rely on the compiler behavior of handling top-level “do” forms in a special way: evaluating each of the contained forms sequentially at the top level, effectively removing the nesting and allowing each contained form to be compiled and evaluated before compiling the next one. Macro expansion facilities other than the compiler (e.g., in an editor or debugger) may not duplicate that subtle behavior so the expansions they produce may not accurately reflect the expansion that will be produced and seen by the compiler. There may be contexts where that causes trouble at development time.
I’m not aware of these being used anywhere in other than experimental/POC code.
--
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+unsubscribe@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 the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.