Is Clojure's lack of support for forward referencing a feature or a bug?

6 views
Skip to first unread message

quasar

unread,
Mar 11, 2009, 11:23:32 AM3/11/09
to Clojure
It seems it makes Clojure source code to be in the order of lowest-to-
highest abstraction.
Naive mutual recursion based on top-level functions is impossible.
I am curious, is it due to the current implementaiton of Reader or by
design?

Best regards,
Leonid

pmf

unread,
Mar 11, 2009, 8:10:33 PM3/11/09
to Clojure
You can use
(declare something)

Timothy Pratley

unread,
Mar 11, 2009, 8:18:53 PM3/11/09
to Clojure
Clojure does support forward referencing (if I understand your
question):
user=> (declare a)
user=> (defn b [x] (a x))
user=> (defn a [x] (b x))
user=> (a 4)
java.lang.StackOverflowError
Note: (declare a) is a synonym for (def a) which works also.

It is also quite trivial to patch the compiler to auto-def symbols as
it finds them instead of throwing an error. That makes it hard to
discover typoed symbols. One strategy to have the best of both worlds
would be to allow auto-def until a non-def call is made, and at that
point warn if there are any unbound. Not quite correct for a fully
dynamic language.


Regards,
Tim.

Mark Engelberg

unread,
Mar 26, 2009, 1:15:52 AM3/26/09
to clo...@googlegroups.com
On Wed, Mar 11, 2009 at 5:18 PM, Timothy Pratley
<timothy...@gmail.com> wrote:
> It is also quite trivial to patch the compiler to auto-def symbols as
> it finds them instead of throwing an error.

I would be interested in knowing how to do such a patch. When I work
on code, I like to organize my functions in a way that makes it easy
to read and understand what is going on. As I work on longer chunks
of Clojure code, I'm finding that shuffling around the functions to
avoid a lot of forward declarations is destroying the readability of
my code.

Timothy Pratley

unread,
Mar 26, 2009, 1:42:55 AM3/26/09
to Clojure
Hi Mark,

A fuller discussion can be found here:
http://groups.google.com/group/clojure/browse_thread/thread/a99b420d5ee0aa40/47f8c2ab6845e9ae
Which has links to the simple patch I tried, and discusses the more
advanced technique Laurent experimented with.
Elena subsequently developed an emacs plugin which looks interesting
(I'm a VI ninja though so haven't used it)
http://groups.google.com/group/clojure/browse_thread/thread/ca7076f4c6591fdd/cda5cf10b89a3679

My own experience FWIW was that it was great for two weeks coding with
autodef, then for about a week I became frustrated with my typos and
disabled it. More promising solutions might come from an external tool
(such as Knuth's literate programming noweb) or IDE support like Elena
described.

For now my work flow is write the code backwards (ie: manually move
the cursor up) and/or chopping and pasting. Then when I'm happy with
it, I re-chop it all in my 'preferred' order and put a declare at the
top. That sounds quite inefficient, but VI is really great for re-
organizing text blocks so it is not too strenuous. That said, I'm
really interested in ways that "literate programming" style can be
followed with the least external support.


Regards,
Tim.


On Mar 26, 4:15 pm, Mark Engelberg <mark.engelb...@gmail.com> wrote:
> On Wed, Mar 11, 2009 at 5:18 PM, Timothy Pratley
>

David Nolen

unread,
Mar 26, 2009, 1:47:34 AM3/26/09
to clo...@googlegroups.com
For what it's worth I'm a big fan of the wishful thinking programming style. I write some functions how I think they should look, declare the functions I haven't defined yet. Then I implement the lower level functions, write some tests- then usually the higher level stuff works without too much tweaking.

In a non-functional programming language this doesn't work so well, but it's a good fit for something like Clojure.

Timothy Pratley

unread,
Mar 26, 2009, 1:55:31 AM3/26/09
to Clojure
:) I like that description! :)
Reply all
Reply to author
Forward
0 new messages