<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged bindings</title>
<link>https://ask.clojure.org/index.php/tag/bindings</link>
<description></description>
<item>
<title>Using letfn* for defining corecursive reified implementations</title>
<link>https://ask.clojure.org/index.php/14592/using-letfn-defining-corecursive-reified-implementations</link>
<description>&lt;p&gt;I was finding a way to have co-recursive reified instances, and after looking at the implementation of &lt;code&gt;clojure.lang.Compiler&lt;/code&gt; I've found out that this can actually be achieved with &lt;code&gt;letfn*&lt;/code&gt;. Consider the following proof-of-concept:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defmacro thunk [&amp;amp; body]
  `(reify* [clojure.lang.IFn]
      (invoke [this] ~@body)))

(letfn* [g (thunk (+ 1 2 3 (f)))
         f (thunk (+ 1 2 3))]
    (f))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works, and seems to be faster than other state-based approaches (e.g. using &lt;code&gt;atom&lt;/code&gt; to resolve forward declarations). &lt;/p&gt;
&lt;p&gt;This said, I recognize that this may not be standard behavior. for instance using &lt;code&gt;(reify* ...)&lt;/code&gt; directly in the &lt;code&gt;letfn*&lt;/code&gt; bindings will cause the &lt;code&gt;reify*&lt;/code&gt; block to be parsed as a &lt;code&gt;MetaExpr&lt;/code&gt; which breaks a cast in the implementation of &lt;code&gt;LetFnExpr&lt;/code&gt;. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(letfn* [g (reify* [clojure.lang.IFn] (invoke [this] (+ 1 2 3 (f))))
         f (reify* [clojure.lang.IFn] (invoke [this] (+ 1 2 3)))] 
  (f))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But since this would implicitly add metadata to the &lt;code&gt;reify*&lt;/code&gt; forms we get this error:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class clojure.lang.Compiler$MetaExpr cannot be cast to class clojure.lang.Compiler$ObjExpr (clojure.lang.Compiler$MetaExpr and clojure.lang.Compiler$ObjExpr are in unnamed module of loader 'app')
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The only way I've managed to create a &lt;code&gt;reify*&lt;/code&gt; form without metadata is by creating the form in macro, such as the &lt;code&gt;thunk&lt;/code&gt; macro defined in the first example.&lt;/p&gt;
&lt;p&gt;Is using &lt;code&gt;reify*&lt;/code&gt; within &lt;code&gt;letfn*&lt;/code&gt; bindings supported? If not, are there any other methods that avoid mutable state that could be employed in similar cases? &lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14592/using-letfn-defining-corecursive-reified-implementations</guid>
<pubDate>Fri, 27 Jun 2025 10:38:18 +0000</pubDate>
</item>
<item>
<title>Returning the last assigned value from `let` w/o a body</title>
<link>https://ask.clojure.org/index.php/13782/returning-the-last-assigned-value-from-let-w-o-a-body</link>
<description>&lt;p&gt;This is more of a suggestion, not really sure where to post it though. Please refer me to the relevant forum if there exists a better one.&lt;/p&gt;
&lt;p&gt;Basically I'm going under the assumption that no one uses &lt;code&gt;let&lt;/code&gt; without a body. The only reason I can imagine for doing that is purely for side-effects, and &lt;code&gt;do&lt;/code&gt; etc. are a much better fit, and currently &lt;code&gt;(let [x 1 y 2 z (+ x y)]) =&amp;gt; nil&lt;/code&gt; (rather than throwing a spec error, as if this is useful expected behavior? But why?)&lt;/p&gt;
&lt;p&gt;In any case, if my assumption is correct, then I propose &lt;code&gt;let&lt;/code&gt; returns the last assigned value, such that &lt;code&gt;(let [x 1 y 2 z (+ x y)]) =&amp;gt; 3&lt;/code&gt;, that is &lt;code&gt;z&lt;/code&gt;. This is ergonomic, and intuitive in my eyes. It's also especially useful for long binding forms building off of the same variables only for the final one to be important, which then requires a whole row with special funky indentation just for that.&lt;/p&gt;
</description>
<category>Macros</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13782/returning-the-last-assigned-value-from-let-w-o-a-body</guid>
<pubDate>Wed, 13 Mar 2024 23:17:32 +0000</pubDate>
</item>
</channel>
</rss>