Question about `binding'

4 views
Skip to first unread message

Yang Dong

unread,
Sep 3, 2010, 11:21:25 PM9/3/10
to Clojure
(binding [inc dec]
(println (inc 1)))

;=> 2

If I put a declare on top of it,

(declare inc)

(binding [inc dec]
(println (inc 1)))

;=> 0

Originally I thought binding is equal to thread local dynamic binding.
Seems I was wrong. It seems like it has something to do with
namespaces. Right?

CuppoJava

unread,
Sep 4, 2010, 9:22:15 AM9/4/10
to Clojure
Your original understanding is correct. Your first example is not
working the way you expect it because Clojure is inlining "inc" for
speed purposes.

Hope that helps
-Patrick

Yang Dong

unread,
Sep 5, 2010, 1:48:27 AM9/5/10
to Clojure
That sounds reasonable. But how would you explain `pmap'? This
function is not inlined. In fact, every function in clojure.core can't
be bound without a declare (as far as I have tried). And it seems this
problem only exists when I tried to bind the core functions. It's ok
for me to substitute the functions in other namespaces.

Michał Marczyk

unread,
Sep 5, 2010, 6:32:25 PM9/5/10
to clo...@googlegroups.com
On 5 September 2010 07:48, Yang Dong <ydong....@gmail.com> wrote:
> That sounds reasonable. But how would you explain `pmap'? This
> function is not inlined. In fact, every function in clojure.core can't
> be bound without a declare (as far as I have tried). And it seems this
> problem only exists when I tried to bind the core functions. It's ok
> for me to substitute the functions in other namespaces.

When the compiler encounters a non-namespace-qualified symbol in the
code, it first resolves it to a Var. For example, the symbol 'inc
normally gets resolved to #'clojure.core/inc; however, after you
(declare inc) in the user namespace, it resolves to #'user/inc. Calls
going through Vars in clojure.core receive special treatment for
speed, which is why you can't rebind them.

Note that with the upcoming :static functions we'll all be free to
have our Vars behave this way (at a finer, per Var granularity level).

Sincerely,
Michał

Phil Hagelberg

unread,
Sep 6, 2010, 12:02:25 AM9/6/10
to clo...@googlegroups.com
On Sun, Sep 5, 2010 at 3:32 PM, Michał Marczyk <michal....@gmail.com> wrote:
> Calls going through Vars in clojure.core receive special treatment for
> speed, which is why you can't rebind them.

Actually this was backed out before 1.2 was released. Now only
definline functions and (in 1.3) static functions behave this way if I
remember correctly.

-Phil

Reply all
Reply to author
Forward
0 new messages