On 11 Nov, 06:24, Paul Barry <[EMAIL PROTECTED]> wrote:
> In Common Lisp and Scheme, if you have an expression that evaluates a
> symbol, it doesn't evaluate it until you call the function, not when
> you define it.  So you can do this:
>
> Common Lisp:
> [1]> (defun b () a)
> B
> [2]> (defvar a 5)
> A
> [3]> (b)
> 5
>
> Scheme:
> 1 ]=> (define (b) a)
> ;Value: b
> 1 ]=> (define a 5)
> ;Value: a
> 1 ]=> (b)
> ;Value: 5
>
> But you can't do this in Clojure:
> user=> (defn b [] a)
> java.lang.Exception: Unable to resolve symbol: a in this context
> (NO_SOURCE_FILE:1)
>
> But if you def a to something, you can then redef it and it will use
> the value defined later:
> user=> (def a nil)
> #=(var user/a)
> user=> (defn b [] a)
> #=(var user/b)
> user=> (def a 5)
> #=(var user/a)
> user=> (b)
> 5
>
> So is there a reason that Clojure tries to resolve symbols when you
> define a function?  The downside of this is that when you have a file
> with multiple functions in it that call each other, you have to make
> sure they are defined in order of dependency.

I may not be the right person to answer you because I don't know the
exact reason.
It probably has something to do with interop with JVM calling
mechanism and the fact that Java does not allow calling of undefined
functions?

But like you discovered yourself you can do a forward declaration and
then redef your function so I guess that is the workaround I would
suggest.

And unless you have a circular dependency it's not really a problem,
just define them in order.

/Markus

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to