Message from discussion
Fail to run dynamic binding code with Clojure1.4
Received: by 10.68.223.72 with SMTP id qs8mr350083pbc.8.1351654784723;
Tue, 30 Oct 2012 20:39:44 -0700 (PDT)
X-BeenThere: clojure@googlegroups.com
Received: by 10.68.234.37 with SMTP id ub5ls4622741pbc.6.gmail; Tue, 30 Oct
2012 20:39:36 -0700 (PDT)
Received: by 10.68.230.33 with SMTP id sv1mr10953482pbc.18.1351654776595;
Tue, 30 Oct 2012 20:39:36 -0700 (PDT)
Date: Tue, 30 Oct 2012 20:39:36 -0700 (PDT)
From: Satoru Logic <satorulo...@gmail.com>
To: clojure@googlegroups.com
Message-Id: <c1b88aac-d09d-43c3-9302-b70f48308928@googlegroups.com>
In-Reply-To: <E8BE7ECA-746D-4DFB-A129-54FCFC6FFD89@gmail.com>
References: <50a34e88-b211-46d8-be9f-bc6ed09b8104@googlegroups.com>
<E8BE7ECA-746D-4DFB-A129-54FCFC6FFD89@gmail.com>
Subject: Re: Fail to run dynamic binding code with Clojure1.4
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_259_30510290.1351654776289"
------=_Part_259_30510290.1351654776289
Content-Type: multipart/alternative;
boundary="----=_Part_260_7628888.1351654776289"
------=_Part_260_7628888.1351654776289
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Does this mean vars are dynamic by default prior to Clojure1.3, and lexical
afterwards?
On Wednesday, October 31, 2012 9:36:47 AM UTC+8, Andy Fingerhut wrote:
>
> The code works as written in Clojure 1.2 and 1.2.1.
>
> It doesn't in 1.3 and later, unless you change the definition of twice to
> annotate that it is a dynamic var, like so:
>
> (defn ^:dynamic twice [x]
> (println "original function")
> (* 2 x))
>
> With that change, it works in Clojure 1.3 and later.
>
> Andy
>
>
> On Oct 30, 2012, at 5:42 PM, Satoru Logic wrote:
>
> Hi, all.
>
> I am reading Clojure in Action.
>
> In the "scope" section of Chapter3, there are examples like this:
>
> defn twice [x]
> (println "original function")
> (* 2 x))
>
> (defn call-twice [y]
> (twice y))
>
> (defn with-log [function-to-call log-statement]
> (fn [& args]
>
> (println log-statement)
> (apply function-to-call args)))
>
> (call-twice 10)
>
> (binding [twice (with-log twice "Calling the twice function")]
> (call-twice 20))
>
> (call-twice 30)
>
>
> When I tried to run this code in repl, I got the following exception:
>
>
> IllegalStateException Can't dynamically bind non-dynamic var: user/twice
>> clojure.lang.Var.pushThreadBindings (Var.java:353)
>
>
> Is the book wrong or I'm running with the wrong version of clojure?
>
>
------=_Part_260_7628888.1351654776289
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Does this mean vars are dynamic by default prior to Clojure1.3, and lexical=
afterwards?<br><br>On Wednesday, October 31, 2012 9:36:47 AM UTC+8, Andy F=
ingerhut wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-=
left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div style=3D"w=
ord-wrap:break-word">The code works as written in Clojure 1.2 and 1.2.1.<di=
v><br></div><div>It doesn't in 1.3 and later, unless you change the definit=
ion of twice to annotate that it is a dynamic var, like so:</div><div><br><=
/div><div><div>(defn ^:dynamic twice [x]</div><div> (println "origina=
l function")</div><div> (* 2 x))</div><div><br></div><div>With that c=
hange, it works in Clojure 1.3 and later.</div><div><br></div><div>Andy</di=
v><div><br></div><div><br></div><div><div>On Oct 30, 2012, at 5:42 PM, Sato=
ru Logic wrote:</div><br><blockquote type=3D"cite">Hi, all.<div><br></div><=
div>I am reading Clojure in Action.</div><div><br></div><div>In the "scope"=
section of Chapter3, there are examples like this:</div><div><br></div><di=
v>
=09=09
=09
=09
=09=09<div title=3D"Page 99">
=09=09=09<div>
=09=09=09=09<div>
=09=09=09=09=09<pre><span style=3D"font-size:8.000000pt;font-family:'Courie=
r'">defn twice [x]
(println "original function")
(* 2 x))
</span></pre>
=09=09=09=09=09<pre><span style=3D"font-size:8.000000pt;font-family:'Courie=
r'">(defn call-twice [y]
(twice y))
</span></pre>
=09=09=09=09=09<pre><span style=3D"font-size:8.000000pt;font-family:'Courie=
r'">(defn with-log [function-to-call log-statement]
(fn [& args]</span>=09=09=09=09</pre><pre><div title=3D"Page 100"><di=
v><div><pre><span style=3D"font-size:8.000000pt;font-family:'Courier'"> =
(println log-statement)
(apply function-to-call args)))
</span></pre>=09=09=09=09<pre><span style=3D"font-size:8.000000pt;font-fami=
ly:'Courier'">(call-twice 10)
</span></pre>=09=09=09=09=09<pre><span style=3D"font-size:8.000000pt;font-f=
amily:'Courier'">(binding [twice (with-log twice "Calling the twice functio=
n")]
(call-twice 20))
</span></pre>=09=09=09=09<pre><span style=3D"font-size:8.000000pt;font-fami=
ly:'Courier'">(call-twice 30)
</span></pre><pre><span style=3D"font-size:8.000000pt;font-family:'Courier'=
"><br></span></pre><pre><font face=3D"Courier"><span style=3D"font-size:11p=
x">When I tried to run this code in repl, I got the following exception:</s=
pan></font></pre><pre><font face=3D"Courier"><span style=3D"font-size:11px"=
><br></span></font></pre><blockquote class=3D"gmail_quote" style=3D"margin:=
0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);=
border-left-style:solid;padding-left:1ex"><font face=3D"Courier"><span styl=
e=3D"font-size:11px">IllegalStateException Can't dynamically bind non-dynam=
ic var: user/twice clojure.lang.Var.<wbr>pushThreadBindings (Var.java:353)=
</span></font></blockquote><div><br></div><div>Is the book wrong or I'm run=
ning with the wrong version of clojure? </div></div></div></div></pre>=
</div></div></div></div></blockquote></div></div></div></blockquote>
------=_Part_260_7628888.1351654776289--
------=_Part_259_30510290.1351654776289--