Please explain

0 views
Skip to first unread message

timc

unread,
Feb 27, 2009, 4:08:17 AM2/27/09
to Clojure
On the page describing Vars, I cannot get the meaning of this sentence
(a typo has made it incomprehensible I think):

"Bindings created with binding can be assigned to, which provides a
means for nested contexts to communicate with code before it the call
stack."

Thanks

Jason Wolfe

unread,
Feb 27, 2009, 4:23:17 AM2/27/09
to Clojure
Here's an example:

user> (def x)
#'user/x

user> (defn foo [] (set! x 10))
#'user/foo

user> (binding [x 1] [x (binding [x 2] [x (do (foo) x)]) x])
[1 [2 10] 1]

Foo passes information to the calling form by assigning to x, within
the innermost binding only.

It sounds like the quote should say "before it [in] the call stack".

-Jason

Michael Wood

unread,
Feb 27, 2009, 5:34:18 AM2/27/09
to clo...@googlegroups.com

I think there's just an "in" missing. i.e. I think it should be:

Bindings created with binding can be assigned to, which provides a

means for nested contexts to communicate with code before it _in_ the call
stack.

It could be a little clearer as follows:

Bindings created with _the binding macro_ can be assigned to, which[...]

--
Michael Wood <esio...@gmail.com>

Mark Volkmann

unread,
Feb 27, 2009, 7:30:35 AM2/27/09
to clo...@googlegroups.com

The word "before" is what seems out of place to me. The typically use
of binding that I've seen is for binding a different value to a Var
for the duration of the binding scope which includes calls that occur
*after* the binding keyword, but in its scope. For example, println
normally writes to stdout, but you can changes that for a limited
scope with binding.

(println "This goes to stdout.")

(binding [*out* (java.io.FileWriter. "my.log")]
(println "This goes to the log file.")
(foo "bar") ; If the function foo uses println, that output will
also go to the log file.
(flush))

(println "Now we're back to writing to stdout.")

--
R. Mark Volkmann
Object Computing, Inc.

Michael Wood

unread,
Feb 27, 2009, 8:42:37 AM2/27/09
to clo...@googlegroups.com
On Fri, Feb 27, 2009 at 2:30 PM, Mark Volkmann
<r.mark....@gmail.com> wrote:
>
> On Fri, Feb 27, 2009 at 4:34 AM, Michael Wood <esio...@gmail.com> wrote:
[...]

>> Bindings created with binding can be assigned to, which provides a
>> means for nested contexts to communicate with code before it _in_ the call
>> stack.
[...]
>
> The word "before" is what seems out of place to me. The typically use
> of binding that I've seen is for binding a different value to a Var
> for the duration of the binding scope which includes calls that occur
> *after* the binding keyword, but in its scope. For example, println
> normally writes to stdout, but you can changes that for a limited
> scope with binding.
[...]

I actually didn't think too hard about that before your e-mail :) but
I think "before" is right. Jason's example demonstrates what this
means:

user> (def x)
#'user/x

user> (defn foo [] (set! x 10))
#'user/foo

user> (binding [x 1] ; first binding form
[x (binding [x 2] ; second binding form


[x (do (foo) x)]) x])
[1 [2 10] 1]

The second binding form is "before" foo in the call stack, yet foo can
influence it by calling (set! x 10).

I'm not sure if doing this sort of thing is a good idea, though.

While reading the description again I noticed a grammatical error. I
believe it should be:


Bindings created with binding can be assigned to, which provides a

means for a nested context to communicate with code before it in the call
stack.

i.e. "a ... context ... before it" rather than "contexts ... before it"

--
Michael Wood <esio...@gmail.com>

Reply all
Reply to author
Forward
0 new messages