Alyssa Kwan <alyssa.c.k...@gmail.com> writes:

> I understand exactly why this situation exists.  I just think the
> behavior is unexpected.  When I create a function with a dynamic
> binding, I expect the function to keep a reference to the *name*, not
> the var that the name resolves to at compile/clinit time.

Oh, I see what you mean.  I guess you're expecting something more like
Python's behaviour: 

    >>> x = 5
    >>> def foo():
    ...     return x
    ... 
    >>> foo()
    5
    >>> del x
    >>> foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in foo
    NameError: global name 'x' is not defined
    
In the case of Python, globals are a mutable map of names directly to
values and are presumably looked up at runtime.  Python doesn't have
Clojure's concept of "vars".

    >>> globals()
    {..., 'x': 5, 'foo': <function foo at 0xb775b7d4>, ...}
    
> I guess the question is:  what do other people expect?  Am I alone in
> thinking that this is unexpected and undesirable?

It makes sense to me.  I have a mental picture of functions closing over
the (lexical) environment as it existed when the function was defined
and that includes the dynamic vars as they were named at that moment.

Similarly in Python you can do this:

    def foo():
        return bar()
    
    def bar():
        return 5

Whereas in Clojure you would need to declare bar before foo.

That may mean dynamic vars are not exactly the same thing as traditional
dynamic scoping, but I don't see anything obviously unexpected or
undesirable about it.  In fact quite the opposite, it's an intentional
design choice.  It's consistent with the Clojure philosophy of
identities being first class.  One of the major themes that distinguishes
Clojure's programming model from traditional languages is that names,
identities and values are distinct concepts.

A single var can be mapped into different namespaces under different
names (for example using the :rename argument to "use").  So the same
identity (var) may be referred to by multiple names (symbols), but it's
the identity you are dynamically binding a value to, not the name.

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to