You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: clojure+/print/objects_and_protocols.html
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -399,20 +399,20 @@ <h1 class="title">Printing Objects in Clojure</h1>
399
399
<spanid="cb1-2"><ahref="#cb1-2" aria-hidden="true" tabindex="-1"></a> (<spanclass="va">#'clojure.core/print-object</span> x w))</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>The syntax is <code>#object[CLASS-NAME HASH toString())]</code> and as you can see, the toString of an Object is <code>CLASS-NAME@HASH</code>. This can get pretty ugly:</p>
411
411
<divclass="sourceClojure">
412
412
<divclass="sourceCode" id="cb5"><preclass="sourceCode clojure code-with-copy"><codeclass="sourceCode clojure"><spanid="cb5-1"><ahref="#cb5-1" aria-hidden="true" tabindex="-1"></a>(async/chan)</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
<spanid="cb1-2"><ahref="#cb1-2" aria-hidden="true" tabindex="-1"></a> (<spanclass="va">#'clojure.core/print-object</span> x w))</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>The syntax is <code>#object[CLASS-NAME HASH toString())]</code> and as you can see, the toString of an Object is <code>CLASS-NAME@HASH</code>. For most objects this becomes quite a long string.</p>
418
418
<divclass="sourceClojure">
419
419
<divclass="sourceCode" id="cb5"><preclass="sourceCode clojure code-with-copy"><codeclass="sourceCode clojure"><spanid="cb5-1"><ahref="#cb5-1" aria-hidden="true" tabindex="-1"></a>(async/chan)</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
<p>It’s quite easy to miss the fact that it is a function as we are looking for a tiny little <code>fn</code> in a sea of text. If, like me, you are fond of the <ahref="../../code_interview/beating/with_stupid_stuff/z_combinator_gambit.html">odd lambda calculus excursion</a>, things get even more hectic.</p>
<p>Yikes! what an eyesore. This is not an academic issue specific to lambda calculus. Any function created from inside a function is helpfully identifiable through the <code>fn$fn</code> nesting. We create these quite regularly, and they are often printed in stack traces. I’m sure you have seen them when you map an inline function across a seq, and there is a bug in the anonymous function.</p>
<spanid="cb13-4"><ahref="#cb13-4" aria-hidden="true" tabindex="-1"></a> [clojure.core$mapv$fn__8565 invoke <spanclass="st">"core.clj"</span><spanclass="dv">7059</span>]]</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
459
459
</div>
460
-
<p>See that part <code>caesar_cipher$add2</code>? That is <strong>very</strong> useful information. It tells us that the exception was inside <code>add2</code>, which is inside <code>caesar-cipher</code>. The stack trace doesn’t print functions as objects, I’m just pointing out that the thing that we care about for functions is really just that they are a function, what their name is, and whether they were created from inside another function. Let’s go back to why printing a function as an object. An easy improvement is to demunge from Java names to Clojure names. Demunging converts <code>_</code> to <code>-</code> and <code>$</code> to <code>/</code>, and munged characters like <code>+</code> which is <code>PLUS</code> in Java.</p>
460
+
<p>See that part <code>caesar_cipher$add2</code>? That is <strong>very</strong> useful information. It tells us that the exception was inside <code>add2</code>, which is inside <code>caesar-cipher</code>. The stack trace doesn’t print functions as objects, but it illustrates that the thing that we care about is that they are a function, what their name is, and whether they were created from inside another function.</p>
461
+
<p>Let’s return to printing a function as an object. An easy improvement is to demunge from Java names to Clojure names. Demunging converts <code>_</code> to <code>-</code> and <code>$</code> to <code>/</code>, and munged characters like <code>+</code> which is <code>PLUS</code> in Java.</p>
<divclass="sourceCode" id="cb19"><preclass="sourceCode clojure code-with-copy"><codeclass="sourceCode clojure"><spanid="cb19-1"><ahref="#cb19-1" aria-hidden="true" tabindex="-1"></a><spanclass="st">"clojure.print-object.remove-extraneous/fn/fn"</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
488
489
</div>
489
-
<p>Much nicer. I can actually read that! I’m not particularly fond of the long namespace shown, and multiple slashes form invalid symbols, so I prefer using $ as the name level delimiter.</p>
490
+
<p>Much nicer. I can actually read that! I’m not particularly fond of the long namespace shown as the name is either defined in this namespace, referred, or part of <code>clojure.core</code>. The multiple slashes form invalid symbols which annoy me; I prefer using <code>/</code> only for <code>namespace/name</code> separation and <code>$</code> as the name level delimiter: <code>my.namespace/my$nested$name</code>.</p>
0 commit comments