<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged locking</title>
<link>https://ask.clojure.org/index.php/tag/locking</link>
<description></description>
<item>
<title>volatile-mutable not recognized in some deftype method subforms</title>
<link>https://ask.clojure.org/index.php/12491/volatile-mutable-recognized-some-deftype-method-subforms</link>
<description>&lt;p&gt;This is a somewhat contrived example but there is a curious behavior that may or may not be a bug.&lt;/p&gt;
&lt;p&gt;Suppose you have the following protocol:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defprotocol P (f [this]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And concrete type with a mutable parameter and lock:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(deftype T [^:volatile-mutable x lock]
  P
  (f [this]
    (locking lock
       (set! x true)
       (prn &quot;&quot;))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Compiler has no problem with this. But move the &lt;code&gt;(prn &quot;&quot;)&lt;/code&gt; statement outside the &lt;code&gt;(locking …)&lt;/code&gt; block and you get:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(deftype T [^:volatile-mutable x lock]
  P
  (f [this]
    (locking lock
      (set! x true))
    (prn &quot;&quot;)))

Syntax error (IllegalArgumentException) compiling fn* at (REPL:4:5).
Cannot assign to non-mutable: x
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;David Miller spotted this behavior in the lastest ClojureCLR (see &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/CLJCLR-122&quot;&gt;https://clojure.atlassian.net/browse/CLJCLR-122&lt;/a&gt;) and is sleuthing it on his end but the fact the same error is raised in JVM Clojure suggests that either there is a bug in Clojure proper, or some highly counterintuitive rule is at work here, the details of which are probably worth understanding. Why the compiler would view x as non-mutable here is not clear.&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12491/volatile-mutable-recognized-some-deftype-method-subforms</guid>
<pubDate>Wed, 21 Dec 2022 17:46:19 +0000</pubDate>
</item>
</channel>
</rss>