On 27 Nov., 13:06, Meikel Brandmeyer <[EMAIL PROTECTED]> wrote:

Hello.

> Maybe I have a wrong understanding of "closure", but
> as far as I understand, every function definition in
> Clojure - be it defn or #() - finally translates to (fn ...).
> And fn creates a closure.

Well, from my experience I can say that a lot of people
think that “anon function = closure”, which would be
fn in Clojure or lambda in Common Lisp.
This however is not generally correct. In my previous
posting I gave an example of how a named function
became a closure.


> In fact #(prn "Hello") closes
> over the var "prn". Consider the following example:
>
> (ns foo.bar)
>
> (def my-closure #(prn "Hello"))

Inside the namespace foo.bar the function
clojure.core/prn is visible. I think that when you
define my-closure the compiler inserts the hard
address of clojure.core/prn to know where to jump
when called. There is nothing variable bound.
It is the same as if you did that while being in
the clojure.core namespace. “prn” is globally
visible, it is a special variable, not a lexical
one. Your my-closure captures the empty lexical
environment which I would explicitly like to
exclude from a definition of “closure”.


> (ns yoyo.dyne
>   (:refer-clojure
>     :exclude (prn)))
>
> (defn prn [x] (clojure.core/prn (str "Modified: " x)))
>
> (foo.bar/my-closure)
>
> According to my understanding the output should
> be "Hello".

I agree. Having the namespace foo.bar did not
change anything important here, we could have done
the same inside user or clojure.core.
While you said (defn my-clojure ...) you accessed
something from the global namespace.
When someone sees the isolatedcode of a closure this
person would have to ask: “What is this variable x??
I can not see it's definition”, as in:

(fn [n] (+ n x))

x may be a global variable, in which this anon
function would not be a closure. Or this fn is
embedded inside a let or fn or whatever, where
x is lexically visible and the fn implicitly
“inherits” it.


> I think the closure is never "empty". It refers to the
> environment used at the closure's creation time.
> Whether this environment is accessed or not, is not
> very interesting, I think.

As I see it we had examples where the lexical
environment was empty.
Global capture is no magic, but the whole reason for
having global vars.
But even in the case we would ignore that: I would still
vote against calling these guys closures, simply as in
my opinion any definition is not qualified, which implies
that *all* functions are closures. It makes not much sense
to invent this special word and confuse thousands of
people with it who are eager to learn what closures really
are, when they are in the end nothing but functions.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
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