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
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -404,20 +404,20 @@ <h1 class="title">Printing Objects and Protocols in Clojure</h1>
404
404
<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>
416
416
<divclass="sourceClojure">
417
417
<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><ahref="https://github.com/tonsky/clojure-plus">clojure-plus</a> provides print-methods to improve printing many things.</p>
423
423
<divclass="sourceClojure">
@@ -456,7 +456,7 @@ <h1 class="title">Printing Objects and Protocols in Clojure</h1>
456
456
<spanid="cb14-3"><ahref="#cb14-3" aria-hidden="true" tabindex="-1"></a> (.getName))</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
457
457
</div>
458
458
<divclass="printedClojure">
459
-
<divclass="sourceCode" id="cb15"><preclass="sourceCode clojure code-with-copy"><codeclass="sourceCode clojure"><spanid="cb15-1"><ahref="#cb15-1" aria-hidden="true" tabindex="-1"></a><spanclass="st">"clojure_PLUS_.print.objects_and_protocols$eval70765$_PERCENT__PERCENT___70766$_PERCENT__PERCENT__PERCENT___70767"</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
459
+
<divclass="sourceCode" id="cb15"><preclass="sourceCode clojure code-with-copy"><codeclass="sourceCode clojure"><spanid="cb15-1"><ahref="#cb15-1" aria-hidden="true" tabindex="-1"></a><spanclass="st">"clojure_PLUS_.print.objects_and_protocols$eval70766$_PERCENT__PERCENT___70767$_PERCENT__PERCENT__PERCENT___70768"</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
460
460
</div>
461
461
<p>Whoa, that’s pretty gross. We’d prefer to demunge the names at least.</p>
462
462
<divclass="sourceClojure">
@@ -469,7 +469,7 @@ <h1 class="title">Printing Objects and Protocols in Clojure</h1>
469
469
<spanid="cb17-2"><ahref="#cb17-2" aria-hidden="true" tabindex="-1"></a> (class-name))</span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
470
470
</div>
471
471
<divclass="printedClojure">
472
-
<divclass="sourceCode" id="cb18"><preclass="sourceCode clojure code-with-copy"><codeclass="sourceCode clojure"><spanid="cb18-1"><ahref="#cb18-1" aria-hidden="true" tabindex="-1"></a><spanclass="st">"clojure+.print.objects-and-protocols/eval70772/%%--70773/%%%--70774"</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
472
+
<divclass="sourceCode" id="cb18"><preclass="sourceCode clojure code-with-copy"><codeclass="sourceCode clojure"><spanid="cb18-1"><ahref="#cb18-1" aria-hidden="true" tabindex="-1"></a><spanclass="st">"clojure+.print.objects-and-protocols/eval70773/%%--70774/%%%--70775"</span></span></code><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></pre></div>
473
473
</div>
474
474
<p>Notice the <code>/evalNNNNN/</code> part? To create a function, Clojure creates a new class. The <code>/evalNNNNN/</code> counts every time it evaluates. This is useful in the sense that it identifies the class for that evaluation. But we almost never care for that detail (more on that later). For the same reason our strangely named functions have <code>--NNNNN</code> appended to them, because they are sub evaluations of the top-level evaluation.</p>
475
475
<p>Let’s do away with that noise for the moment:</p>
<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>
422
422
<divclass="sourceClojure">
423
423
<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>
class code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_SINGLEQUOTE_\)</span>fn__70990 cannot be cast to class clojure.lang.IPersistentCollection (code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_SINGLEQUOTE_\)</span>fn__70990 is in unnamed module of loader clojure.lang.DynamicClassLoader <spanclass="citation" data-cites="1f484be2">@1f484be2</span>; clojure.lang.IPersistentCollection is in unnamed module of loader ‘app’)
514
+
class code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_SINGLEQUOTE_\)</span>fn__70991 cannot be cast to class clojure.lang.IPersistentCollection (code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_SINGLEQUOTE_\)</span>fn__70991 is in unnamed module of loader clojure.lang.DynamicClassLoader <spanclass="citation" data-cites="210488ba">@210488ba</span>; clojure.lang.IPersistentCollection is in unnamed module of loader ‘app’)
class code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_LOGIC\)</span>fn__71000 cannot be cast to class clojure.lang.IPersistentCollection (code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_LOGIC\)</span>fn__71000 is in unnamed module of loader clojure.lang.DynamicClassLoader <spanclass="citation" data-cites="4ca63de9">@4ca63de9</span>; clojure.lang.IPersistentCollection is in unnamed module of loader ‘app’)
601
+
class code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_LOGIC\)</span>fn__71001 cannot be cast to class clojure.lang.IPersistentCollection (code_interview.beating.with_stupid_stuff.z_combinator_gambit<spanclass="math inline">\(REV_LOGIC\)</span>fn__71001 is in unnamed module of loader clojure.lang.DynamicClassLoader <spanclass="citation" data-cites="5cc46e01">@5cc46e01</span>; clojure.lang.IPersistentCollection is in unnamed module of loader ‘app’)
0 commit comments