<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="https://batsov.com/feeds/Clojure.xml" rel="self" type="application/atom+xml" /><link href="https://batsov.com/" rel="alternate" type="text/html" hreflang="en" /><updated>2026-11-06T10:47:14+02:00</updated><id>https://batsov.com/feeds/Clojure.xml</id><title type="html">(think)</title><subtitle>Bozhidar Batsov&apos;s personal blog</subtitle><entry><title type="html">neat: a language-agnostic nREPL client for Emacs</title><link href="https://batsov.com/articles/2026/05/20/neat-a-language-agnostic-nrepl-client-for-emacs/" rel="alternate" type="text/html" title="neat: a language-agnostic nREPL client for Emacs" /><published>2026-05-20T09:00:00+03:00</published><updated>2026-05-20T09:00:00+03:00</updated><id>https://batsov.com/articles/2026/05/20/neat-a-language-agnostic-nrepl-client-for-emacs</id><content type="html" xml:base="https://batsov.com/articles/2026/05/20/neat-a-language-agnostic-nrepl-client-for-emacs/"><![CDATA[<blockquote>
  <p>I think I’ll take my REPL neat <br />
My parens black and my bed at three <br />
CIDER’s too sweet for me…</p>

  <p>– Bozier</p>
</blockquote>

<p>Last week I <a href="/articles/2026/05/12/port-a-minimalist-prepl-client-for-emacs/">announced Port</a>,
a small prepl client for Emacs. Today I’m following it up with another small
Emacs package. Meet <a href="https://github.com/nrepl/neat">neat</a>, a tiny, deliberately
language-agnostic nREPL client.</p>

<h2 id="why">Why?</h2>

<p>For years I’ve been hearing some version of the same request: “could CIDER work
with my non-Clojure nREPL server?”. Babashka, Basilisp, nREPL-CLR, even some
homegrown servers people built on top of nREPL for languages I’d never heard
of.<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup> The answer was always the same kind of squishy “sort of, in theory, with
caveats”, because while bare nREPL is genuinely language-agnostic, CIDER is
not. CIDER was built for Clojure and assumes Clojure pretty much everywhere.</p>

<p>I always thought the right answer was “let’s gradually make CIDER more
language-agnostic.” That’s the kind of plan that sounds reasonable until
you actually try it.</p>

<p>The thing that pushed me over the edge was, oddly enough, building Port.  Port
is small, focused, and doesn’t try to be CIDER. Working on it for a couple of
weeks reminded me how (deceptively) productive it is to start from a clean slate
when the new requirements don’t match the assumptions baked into a mature
codebase. Trying to retrofit CIDER into a language-agnostic shape would have
meant fighting with every helper that ever assumed <code class="language-plaintext highlighter-rouge">clojure.repl</code> exists, every
middleware contract <code class="language-plaintext highlighter-rouge">cider-nrepl</code> defines, every project-type heuristic that
knows about <code class="language-plaintext highlighter-rouge">deps.edn</code> and <code class="language-plaintext highlighter-rouge">project.clj</code> and nothing else.  A whole lot of “is
the server Clojure, or is it the other thing?”  branches. The Port experience
reaffirmed that the right move for a genuinely different client is a <em>new
project</em>, not a thousand cuts to an existing one.</p>

<p>So <code class="language-plaintext highlighter-rouge">neat</code> was born. The name is short, says what it does (it’s neat, both in
the small-and-tidy sense and in the “no deps, no special assumptions, just the
protocol” sense), and conveniently leaves room for puns I haven’t fully
committed to yet. I might land on a backronym one day. For now it’s just
“neat”.</p>

<h2 id="what-neat-actually-is">What neat actually is</h2>

<p>neat is a small Emacs nREPL client. The code is  split across four files:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">neat-bencode.el</code>: bencode encode/decode.</li>
  <li><code class="language-plaintext highlighter-rouge">neat-client.el</code>: TCP connections, request dispatch, the standard
nREPL ops.</li>
  <li><code class="language-plaintext highlighter-rouge">neat-repl.el</code>: a comint-derived REPL buffer.</li>
  <li><code class="language-plaintext highlighter-rouge">neat.el</code>: the entry point, customization group, and <code class="language-plaintext highlighter-rouge">neat-mode</code>
minor mode for source buffers.</li>
</ul>

<p>It only uses Emacs builtins. There are no external runtime
dependencies, not even on <code class="language-plaintext highlighter-rouge">clojure-mode</code>, because neat doesn’t assume
Clojure on the other end. If you write <code class="language-plaintext highlighter-rouge">clojure-mode</code>, <code class="language-plaintext highlighter-rouge">fennel-mode</code>,
<code class="language-plaintext highlighter-rouge">hy-mode</code>, or anything else that talks nREPL, you turn on <code class="language-plaintext highlighter-rouge">neat-mode</code>
in that buffer and it just works.</p>

<p>The connection routing is also intentionally library-friendly. There’s a
buffer-local <code class="language-plaintext highlighter-rouge">neat-current-connection</code> override so downstream packages can
implement their own routing logic, plus a global default for the simple
“one server at a time” case that most people will want.</p>

<p>Capability discovery is done at connect time via the nREPL <code class="language-plaintext highlighter-rouge">describe</code> op.
neat doesn’t hardcode “this server has completions, this one doesn’t”
assumptions. If the server reports a <code class="language-plaintext highlighter-rouge">completions</code> op, the CAPF backend
lights up (with type annotations next to each candidate, when the server
provides them). If it reports <code class="language-plaintext highlighter-rouge">lookup</code>, eldoc starts working and <code class="language-plaintext highlighter-rouge">M-.</code>
jumps to definitions via an xref backend. If neither is there, you still
get a perfectly serviceable raw REPL.</p>

<h2 id="basic-usage">Basic usage</h2>

<p>Start an nREPL server. Anything that speaks the protocol will do. For a
Clojure server:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre>clj -M:nrepl
# or
bb nrepl-server :port 7888
# or
lein repl :headless :port 7888
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Then in Emacs:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>M-x neat RET localhost RET 7888 RET
</pre></td></tr></tbody></table></code></pre></div></div>

<p>A REPL buffer pops up, the prompt follows the server’s reported
namespace, and you can type expressions at it. Multi-line input works
because <code class="language-plaintext highlighter-rouge">RET</code> only submits when the form parses as balanced under
<code class="language-plaintext highlighter-rouge">neat-repl-input-syntax-table</code> (Emacs Lisp syntax by default, which is
close enough for any Lisp). Input history is persisted across sessions.</p>

<p>If there’s a <code class="language-plaintext highlighter-rouge">.nrepl-port</code> file in the project, the prompt defaults to
its contents, so <code class="language-plaintext highlighter-rouge">M-x neat RET RET</code> is enough to connect.</p>

<p>To evaluate from a source buffer, turn on the minor mode:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>M-x neat-mode
</pre></td></tr></tbody></table></code></pre></div></div>

<p>The familiar bindings are there, intentionally compatible with what CIDER
users expect:</p>

<table>
  <thead>
    <tr>
      <th>Key</th>
      <th>Command</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-e</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-eval-last-sexp</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-c</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-eval-defun</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-r</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-eval-region</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-b</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-eval-buffer</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-l</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-load-buffer-file</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-z</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-switch-to-repl</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-k</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-interrupt-eval</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c M-n</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-set-ns</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">C-c C-d C-d</code></td>
      <td><code class="language-plaintext highlighter-rouge">neat-show-doc-at-point</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">M-.</code></td>
      <td><code class="language-plaintext highlighter-rouge">xref-find-definitions</code></td>
    </tr>
    <tr>
      <td><code class="language-plaintext highlighter-rouge">M-,</code></td>
      <td><code class="language-plaintext highlighter-rouge">xref-go-back</code></td>
    </tr>
  </tbody>
</table>

<p><code class="language-plaintext highlighter-rouge">neat-eval-buffer</code> ships the buffer contents as an <code class="language-plaintext highlighter-rouge">eval</code> op;
<code class="language-plaintext highlighter-rouge">neat-load-buffer-file</code> uses the standard <code class="language-plaintext highlighter-rouge">load-file</code> op instead, so the
server can attribute file and line numbers to errors. Use the latter when
you’re actually loading a file from disk and care about good diagnostics.</p>

<p><code class="language-plaintext highlighter-rouge">neat-set-ns</code> sets the buffer-local <code class="language-plaintext highlighter-rouge">neat-ns</code>, which gets sent as the <code class="language-plaintext highlighter-rouge">ns</code>
field on every <code class="language-plaintext highlighter-rouge">eval</code> op from that buffer. For languages where the
namespace is declared in the source (Clojure’s <code class="language-plaintext highlighter-rouge">(ns foo.bar)</code>, etc.), swap
in a parser via <code class="language-plaintext highlighter-rouge">neat-buffer-ns-function</code>.</p>

<p>For juggling multiple connections, <code class="language-plaintext highlighter-rouge">M-x neat-list-connections</code> opens a
tabulated-list buffer with one row per live connection, where you can
set the default or disconnect interactively.</p>

<p>That’s roughly the whole user-facing surface today. There’s no jack-in
command, no inspector, no debugger, no test runner. Likely there will never
be, but if you need those you should probably be using CIDER anyways…</p>

<h2 id="should-you-use-it">Should you use it?</h2>

<p>If you write Clojure and CIDER works for you, keep using CIDER. It’s
mature, full-featured, and supported, and I’m going to keep working on it
for as long as people use it. Nothing about neat changes that.</p>

<p>But if you find yourself in one of these situations:</p>

<ul>
  <li>you write a non-Clojure language whose runtime ships an nREPL server,
and you’ve been muddling through with a half-supported CIDER setup,</li>
  <li>you write Clojure but you value minimalism and don’t need the full
CIDER feature set,</li>
  <li>you’re building an Emacs package that needs to talk nREPL and you
want a small, dependency-free library to build on,<sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup></li>
</ul>

<p>then neat might be a better fit. It’s small enough that you can read the
whole thing in an afternoon, and the library/UI split (<code class="language-plaintext highlighter-rouge">neat-bencode</code> and
<code class="language-plaintext highlighter-rouge">neat-client</code> are perfectly usable from other packages) is genuinely
designed for downstream consumers.</p>

<h2 id="the-bigger-picture">The bigger picture</h2>

<p>neat is part of a broader push I’ve been chewing on for a while now: making
nREPL a healthy multi-language ecosystem rather than a Clojure-only protocol. That push has three legs:</p>

<ol>
  <li><strong>An actual nREPL specification.</strong> The <a href="https://github.com/nrepl/spec.nrepl.org">spec.nrepl.org</a>
draft is (will be) the formal version of what today is “whatever nREPL the
project does”.</li>
  <li><strong>Reference clients.</strong> neat is one. The point of building a
deliberately Clojure-free client is that it stress-tests the spec.
Anywhere neat ends up needing to special-case the server, the spec
has a gap.</li>
  <li><strong>A compatibility test suite.</strong> The parameterised integration suite
in neat already runs the same assertions against multiple servers
and surfaces real divergences (Clojure batching <code class="language-plaintext highlighter-rouge">(println "hi")</code>
into a single <code class="language-plaintext highlighter-rouge">out</code> message where Basilisp emits two, for example).
I’d like to grow this into a portable suite that any nREPL server
can self-check against.</li>
</ol>

<p>This is also why I keep teasing a “reference CLI client” in
conversations. An editor client is one thing, but a small command-line
nREPL client written in a non-Lisp language would be a much sharper test
of how language-agnostic the protocol really is. neat is plausibly a
precursor to that. Time will tell how far I push this; for now I just
wanted to get the Emacs side moving.</p>

<h2 id="thanks">Thanks</h2>

<p>As always, big thanks to <a href="https://www.clojuriststogether.org/">Clojurists
Together</a> and everyone supporting my open
source work. You make it possible for me to keep tweaking and improving CIDER,
nREPL, clj-refactor, and friends, and occasionally try something “neat” on the
side. <code class="language-plaintext highlighter-rouge">neat</code> isn’t replacing any of the existing Clojure tooling for Emacs. It’s
just another tool in the box for the people who want it.</p>

<p>Feedback, ideas, and contributions are most welcome over at the
<a href="https://github.com/nrepl/neat/issues">issue tracker</a>.</p>

<p>Keep hacking!</p>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p><a href="https://github.com/clojure-emacs/cider/issues/3905">https://github.com/clojure-emacs/cider/issues/3905</a> <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
    <li id="fn:2">
      <p>For a long time I planned to extract CIDER’s nREPL client code into a reusable package, but now that we have <code class="language-plaintext highlighter-rouge">neat</code> I probably will finally abandon this idea. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Bozhidar Batsov</name></author><category term="nREPL" /><category term="Emacs" /><category term="neat" /><category term="Clojure" /><summary type="html"><![CDATA[I think I’ll take my REPL neat My parens black and my bed at three CIDER’s too sweet for me… – Bozier]]></summary></entry><entry><title type="html">nREPL Forever</title><link href="https://batsov.com/articles/2026/05/20/nrepl-forever/" rel="alternate" type="text/html" title="nREPL Forever" /><published>2026-05-20T09:00:00+03:00</published><updated>2026-05-20T09:00:00+03:00</updated><id>https://batsov.com/articles/2026/05/20/nrepl-forever</id><content type="html" xml:base="https://batsov.com/articles/2026/05/20/nrepl-forever/"><![CDATA[<p>Last week I <a href="/articles/2026/05/12/port-a-minimalist-prepl-client-for-emacs/">announced Port</a>,
a small prepl client for Emacs. That post focused on Port itself, but writing it left
me with the itch to do a follow-up on the bigger picture, because the
socket REPL / prepl story is one I’ve been meaning to write up for years.</p>

<p>If you’ve been around Clojure long enough, you remember the chatter. Socket
REPL landed in Clojure 1.8 (January 2016), prepl in Clojure 1.10 (December
2018), and for a couple of years there was a steady stream of posts, tweets,
and Slack threads to the effect of “this is what we should be building
tools on. nREPL is on the way out.” Some serious people put their weight
behind that idea, and some of them went and built tools to prove it.</p>

<p>Now it’s 2026 and we can take stock.</p>

<h2 id="sky-is-the-limit">Sky is the limit</h2>

<p>The pitch was good. Socket REPL is just <em>the</em> Clojure REPL exposed on a TCP
port. prepl wraps it with a structured printer so the bytes coming back are
EDN-tagged maps (<code class="language-plaintext highlighter-rouge">:ret</code>, <code class="language-plaintext highlighter-rouge">:out</code>, <code class="language-plaintext highlighter-rouge">:err</code>, <code class="language-plaintext highlighter-rouge">:tap</code>) instead of a human-readable
prompt. Both ship with Clojure itself. No external server library, no
middleware, no third-party namespaces. You start a JVM, you bind a port,
you’re done.</p>

<p>The intellectual case for moving off nREPL had been made by Rich Hickey
himself, most clearly in a <a href="https://groups.google.com/g/clojure-dev/c/Dl3Stw5iRVA/m/IHoVWiJz5UIJ">March 2015 clojure-dev
post</a>
that’s worth reading in full. Rich didn’t actually attack nREPL by name in
that message. What he did was argue carefully for what a REPL <em>is</em>: a thing
that reads characters, evaluates forms, prints results, and loops, with
those streams available to user code so that things like nested REPLs and
debuggers compose naturally. The money line:</p>

<blockquote>
  <p>While framing and RPC orientation might make things easier for someone
who just wants to implement an eval window, it makes the resulting
service strictly less powerful than a REPL.</p>
</blockquote>

<p>His proposal, in the same post, was that tools should open <em>multiple</em>
connections to the running program: one for the human-facing stream, and
dedicated channels for IDE operations. The socket REPL (which landed in
1.8 the following January) and prepl (which arrived in 1.10) were the
official implementation of that worldview.</p>

<p>A handful of editor projects took the cue and built clients:</p>

<ul>
  <li><a href="https://tutkain.flowthing.me/">Tutkain</a> for Sublime Text</li>
  <li><a href="https://github.com/mauricioszabo/atom-chlorine">Chlorine</a> for Atom</li>
  <li><a href="https://github.com/Olical/conjure">Conjure</a> for Neovim (in its early Rust incarnation)</li>
  <li><a href="https://github.com/tonsky/Clojure-Sublimed">Clojure-Sublimed</a> by Nikita Tonsky</li>
  <li>a steady drip of smaller experiments around <a href="https://github.com/Unrepl/unrepl"><code class="language-plaintext highlighter-rouge">unrepl</code></a>, <a href="https://github.com/Olical/propel"><code class="language-plaintext highlighter-rouge">propel</code></a>, and friends</li>
</ul>

<p>It was real momentum. If you were following Clojure tooling in 2018-2020, it
genuinely felt like nREPL might be the past, and the future would be some
combination of socket REPL plus a thin self-installing protocol on top of it.
You can find a fair number of “RIP nREPL” hot takes from that period if you go
looking.</p>

<h2 id="reality-check">Reality Check</h2>

<p>I went and surveyed each of those projects recently while working on Port.
The pattern is depressingly consistent:</p>

<p><strong>Tutkain</strong> started on prepl. In November 2021, its v0.11 release explicitly
stopped using prepl message framing and switched to a hand-rolled EDN-RPC
protocol that Tutkain boots onto the raw socket REPL by sending it a
base64-encoded blob. The new protocol has request ids, op dispatch
(<code class="language-plaintext highlighter-rouge">:eval</code>, <code class="language-plaintext highlighter-rouge">:lookup</code>, <code class="language-plaintext highlighter-rouge">:completions</code>, <code class="language-plaintext highlighter-rouge">:test</code>, <code class="language-plaintext highlighter-rouge">:apropos</code>, <code class="language-plaintext highlighter-rouge">:interrupt</code>,
…), and server-managed thread bindings. In other words: Tutkain grew
into nREPL, just spelled differently.</p>

<p><strong>Chlorine</strong> never used prepl directly. It used socket REPL plus an
<code class="language-plaintext highlighter-rouge">unrepl</code>-style upgrade blob. Its author’s successor project,
<a href="https://github.com/mauricioszabo/lazuli">Lazuli</a>, abandoned the whole
approach in favor of nREPL. The
<a href="https://mauricio.szabo.link/blog/2024/11/09/goodbye-chlorine-and-welcome-lazuli/">post-mortem</a>
is worth reading and is fairly blunt: tools that attempted prepl went back
to nREPL because, honestly, it’s simply better.</p>

<p><strong>Conjure</strong> had a prepl client in its early Rust days. The current
Lua/Fennel rewrite ships only an nREPL client. The author’s reasoning in
the release notes was that nREPL “has complete ecosystem adoption and
brilliant ClojureScript support.”</p>

<p><strong>Clojure-Sublimed</strong> technically still talks to a raw socket REPL, but only
after sending it an EDN-printing prelude that upgrades the REPL to a
structured protocol of tonsky’s own design. His
<a href="https://tonsky.me/blog/clojure-sublimed-3/">post on the topic</a> is one of
the most thoughtful pieces I’ve read on Clojure REPL design, and his
conclusion is roughly: the bare socket REPL is <em>more</em> useful than prepl
because you can install your own protocol on top of it. Which is true. But
notice that everyone who reached that conclusion ended up reinventing the
same wheel: ids, ops, request/response correlation, completion support,
lookup, interrupts. You know, the things nREPL has had since 2010.</p>

<p>So the trajectory looks roughly like this:</p>

<ol>
  <li>Editor decides nREPL is too heavy or an undesirable external dependency and starts on prepl.</li>
  <li>Editor discovers prepl has no ids, no ops, no interrupts, no server-side
completion, no namespace tracking, no test runner integration, etc.</li>
  <li>Editor rolls a custom protocol on top of socket REPL, or…</li>
  <li>Editor gives up and goes to nREPL.</li>
</ol>

<p>Pure prepl clients are nearly extinct in the wild. The one I found that
qualifies is <a href="https://github.com/Olical/propel">propel</a> by Oliver Caldwell
(of Conjure fame), which is delightful, about 70 lines of Clojure, and
explicitly synchronous (one outstanding eval at a time). That works! But
it’s not a foundation for the kind of feature set people expect from an
editor.</p>

<h2 id="real-repl-vs-tool-friendly-repl">“Real” REPL vs. tool-friendly REPL</h2>

<p>Here’s where I land. Rich isn’t wrong that prepl is closer to a “real” REPL
in the strict sense. prepl genuinely is a more faithful encoding of
read-eval-print: each form goes in, each result comes out, and the semantics
match what you’d get at the standard REPL prompt.</p>

<p>The thing is, “real REPL” is not the property you optimize for when you’re
building editor tooling. The properties editor tooling actually needs are:</p>

<ul>
  <li>A way to <strong>correlate</strong> a request with its response when output and results
are interleaved.</li>
  <li>A way to <strong>multiplex</strong> – one connection, several logical conversations.</li>
  <li><strong>Server-side hooks</strong> for the operations every IDE expects: completion,
lookup, go-to-definition, find-references, test running, stacktrace
structuring, interrupt.</li>
  <li>A <strong>protocol stable enough</strong> that ten different editors can target it
without each one inventing its own dialect.</li>
</ul>

<p>nREPL was <em>explicitly designed</em> for those properties. The
<a href="https://nrepl.org/nrepl/design/overview.html">ops, middleware, and transport</a>
abstractions exist precisely because the people building it knew the consumers
are not humans typing at a prompt, they’re programs negotiating a session.</p>

<p>Calling nREPL “not a real REPL” is technically defensible and practically
beside the point. Nobody on the consuming end is confused about what nREPL
is <em>for</em>.</p>

<h2 id="taking-stock-eight-years-later">Taking stock, eight years later</h2>

<p>I <a href="https://metaredux.com/posts/2018/10/29/nrepl-redux.html">wrote about nREPL’s revival in 2018</a>.
At that point I had just finished migrating the project out of Clojure Contrib,
and the goal was to give it a real home and a working development process. It
was a lot of work, but in hindsight things played out pretty well.</p>

<p>Looking at where things ended up:</p>

<ul>
  <li><strong>nREPL itself</strong> is healthier than it has ever been. Active maintainers, a
proper <a href="https://nrepl.org">manual</a>, a steady release cadence, an actual
ecosystem organization on GitHub.</li>
  <li><strong>Most popular Clojure editors</strong> support it. <a href="https://github.com/clojure-emacs/cider">CIDER</a>,
<a href="https://calva.io">Calva</a>, <a href="https://cursive-ide.com">Cursive</a> (via its
own client), Conjure, <a href="https://github.com/liquidz/vim-iced">vim-iced</a>,
you name it.</li>
  <li><strong><a href="https://babashka.org">babashka</a></strong> ships with nREPL built in. You boot a
<code class="language-plaintext highlighter-rouge">bb</code> and you get an nREPL server, no extra dependencies. That’s how a lot
of people use nREPL in scripting contexts today, and it’s been a hit.</li>
  <li><strong><a href="https://github.com/basilisp-lang/basilisp">basilisp</a></strong> (the Clojure
dialect on Python) has <a href="https://github.com/basilisp-lang/basilisp-nrepl-async">nREPL support</a>.
nREPL running on Python, talking to Emacs, evaluating Clojure. Nice.</li>
  <li><strong><a href="https://github.com/clojure/clojure-clr">ClojureCLR</a></strong> has a working nREPL
story now, and <strong><a href="https://jank-lang.org">jank</a></strong> (the C++ Clojure) has nREPL
on its roadmap too.</li>
  <li>The middleware ecosystem (<a href="https://github.com/clojure-emacs/cider-nrepl"><code class="language-plaintext highlighter-rouge">cider-nrepl</code></a>,
<a href="https://github.com/clojure-emacs/refactor-nrepl"><code class="language-plaintext highlighter-rouge">refactor-nrepl</code></a>,
<a href="https://github.com/nrepl/piggieback"><code class="language-plaintext highlighter-rouge">piggieback</code></a>,
<a href="https://github.com/clojure-emacs/sayid"><code class="language-plaintext highlighter-rouge">sayid</code></a>,
<a href="https://github.com/nrepl/drawbridge"><code class="language-plaintext highlighter-rouge">drawbridge</code></a>, …) is alive, well,
and continues to add features.</li>
</ul>

<p>Meanwhile prepl is, as best as I can tell, mostly a curiosity. It got me a
side project I had fun with. It did not displace nREPL.</p>

<h2 id="epilogue">Epilogue</h2>

<p>The history of tooling protocols is full of cases where “purer”, “simpler”,
or “more elegant” lost to “shipped, documented, and battle-tested.” LSP
beat fifteen ad-hoc language protocols. DAP beat the same fifteen
debuggers. nREPL beat prepl in the (Clojure) editor space.</p>

<p>It’s not that the simpler thing is bad. prepl is a fine, elegant little
protocol, and there’s a real case for embedding it in CI scripts, ops
automation, deployment pipelines, or anywhere you want to drive a Clojure
VM programmatically without pulling in a server library. Use it there.</p>

<p>But for editor tooling? The Clojure community made an enormous, multi-year,
multi-tool investment in nREPL. We have the protocol, the middleware, the
manual, the books, the conference talks. nREPL works, it’s actively
maintained, it’s increasingly portable across Clojure dialects, and the
design decisions that Rich called out as un-REPL-like are the exact ones
that make it a good substrate for editors.</p>

<p>So I’ll say what I felt awkward saying back in 2018: nREPL forever. It’s
the right abstraction for the job, and it’s not going anywhere.</p>

<p>One more thing. After finishing Port I got curious what a minimal nREPL
client would look like by comparison, so I went and built one. As you can
imagine, it turned out to be significantly simpler. If that sounds
interesting, take a look at
<a href="/articles/2026/05/20/neat-a-language-agnostic-nrepl-client-for-emacs/">neat</a>,
a small, language-agnostic nREPL client for Emacs.</p>

<p>Keep hacking!</p>]]></content><author><name>Bozhidar Batsov</name></author><category term="Clojure" /><category term="nREPL" /><category term="prepl" /><category term="Meta" /><summary type="html"><![CDATA[Last week I announced Port, a small prepl client for Emacs. That post focused on Port itself, but writing it left me with the itch to do a follow-up on the bigger picture, because the socket REPL / prepl story is one I’ve been meaning to write up for years.]]></summary></entry><entry><title type="html">Port: a minimalist prepl client for Emacs</title><link href="https://batsov.com/articles/2026/05/12/port-a-minimalist-prepl-client-for-emacs/" rel="alternate" type="text/html" title="Port: a minimalist prepl client for Emacs" /><published>2026-05-12T09:00:00+03:00</published><updated>2026-05-12T09:00:00+03:00</updated><id>https://batsov.com/articles/2026/05/12/port-a-minimalist-prepl-client-for-emacs</id><content type="html" xml:base="https://batsov.com/articles/2026/05/12/port-a-minimalist-prepl-client-for-emacs/"><![CDATA[<p>For ages I’ve had “add prepl support to CIDER” sitting somewhere in the back of
my head. CIDER is built firmly around nREPL, but
<a href="https://clojure.org/reference/repl_and_main#prepl">prepl</a> ships with Clojure
itself, and the appeal of dropping the external REPL server requirement is
obvious.  Recently, as part of a broader internals cleanup “mini-project” in
CIDER, I finally sat down and put a prototype together:
<a href="https://github.com/clojure-emacs/cider/pull/3899">cider#3899</a>.</p>

<p>The good news is that the prototype sort of worked. The bad news is that the
more I poked at it, the more I kept running into the same pattern. CIDER
assumes ops, sessions, request ids, and a whole structured protocol that
prepl simply doesn’t have. The amount of CIDER code that would need to grow
“is this nREPL or prepl?” branches added up quickly, and I’d be papering
over prepl’s limitations in dozens of subtle places. The exercise was fun,
but it ended up reaffirming my long-standing belief that nREPL is a much
better fit for editor tooling than prepl is.</p>

<p>The exercise did leave me thinking though. What if, instead of bolting prepl
onto CIDER, I built a small standalone client in the spirit of
<a href="https://github.com/clojure-emacs/inf-clojure">inf-clojure</a> and
<a href="https://github.com/sanel/monroe">monroe</a>? Something tiny and focused that
doesn’t have to pretend to be CIDER, and where prepl’s quirks would be the
design rather than something to work around.</p>

<p>Conveniently, I was on vacation in Portugal at the time, where I spent a few
days in Porto, and the name pretty much picked itself.
<a href="https://github.com/clojure-emacs/port">Port</a> was born. It kept us firmly in the
land of fun, drink-inspired Clojure-on-editor names: CIDER,
<a href="https://calva.io">Calva</a> (after Calvados, the apple brandy), and now Port (the
famous fortified wine). The protocol Port talks to is <code class="language-plaintext highlighter-rouge">prepl</code>, over a TCP
<strong>port</strong>, so the pun was hard to pass up.</p>

<p>This time around I didn’t manage to land on a backronym I love (at least not
yet). The contenders so far:</p>

<ul>
  <li>prepl omnipotent repl toolkit (my favorite so far)</li>
  <li>prepl-operated repl toolkit</li>
  <li>peak optimized repl toolkit</li>
</ul>

<p>Naming is hard. I remain open to better suggestions. :D</p>

<h2 id="scope-or-what-port-isnt">Scope, or what Port isn’t</h2>

<p>Port is a side project. I don’t plan to invest serious time in it past the
point I consider it feature-complete, which won’t be far beyond what’s
already there. The deliberate goal is to keep it simple and focused, and its
feature set will stay close to inf-clojure and monroe. <strong>Port is not
competing with CIDER.</strong> If you want the full feature set (debugger,
inspector, test runner, profiler, structured stacktraces, refactor support),
CIDER + <code class="language-plaintext highlighter-rouge">cider-nrepl</code> is, and will remain, the way.</p>

<p>What Port gives you today is a small, dependable Clojure REPL that you can
hook into Emacs without any external dependencies, just a stock Clojure JVM
with a prepl listening on a port.</p>

<h2 id="a-few-architectural-notes">A few architectural notes</h2>

<p>If you’re up for the long version, <a href="https://github.com/clojure-emacs/port/blob/main/doc/design.md">doc/design.md</a>
goes deep. Here’s the short version of what prepl gives you compared to nREPL:</p>

<ul>
  <li><strong>No bencode</strong>. prepl emits EDN-tagged maps, one per line. This might be a feature or a problem, depending on your perspective.</li>
  <li><strong>No middleware</strong>. Whatever the server prints is what you get. No
interception, no extension surface.</li>
  <li><strong>No sessions</strong>. There’s one thread per TCP connection.</li>
  <li><strong>No ops</strong>. You send a Clojure form, the server evaluates it, and prints
back tagged messages: <code class="language-plaintext highlighter-rouge">:ret</code>, <code class="language-plaintext highlighter-rouge">:out</code>, <code class="language-plaintext highlighter-rouge">:err</code>, <code class="language-plaintext highlighter-rouge">:tap</code>, plus an <code class="language-plaintext highlighter-rouge">:exception
true</code> flag on errors.</li>
  <li><strong>No request id</strong>. This is the main issue. Tags identify the <em>kind</em> of
message, not which request produced it.</li>
</ul>

<p>That last point is the central design constraint. If you want to issue a
request and reliably read back its result without accidentally consuming
output from an unrelated background <code class="language-plaintext highlighter-rouge">future</code> or <code class="language-plaintext highlighter-rouge">tap&gt;</code>, you need to layer
correlation on top of the protocol. Port does this with two tricks:</p>

<ol>
  <li><strong>Two sockets per session.</strong> A user socket drives the REPL buffer with
raw streaming output, and a separate tool socket carries helper-command
requests. Background prints from <code class="language-plaintext highlighter-rouge">future</code>/<code class="language-plaintext highlighter-rouge">agent</code> on the user thread
don’t bleed into the tool channel.</li>
  <li><strong>A bootstrap form.</strong> On connect, the tool socket evaluates a one-shot
form that defines a <code class="language-plaintext highlighter-rouge">port.tooling/-eval</code> wrapper. Every subsequent helper
call goes through it, which captures <code class="language-plaintext highlighter-rouge">*out*</code> / <code class="language-plaintext highlighter-rouge">*err*</code> and returns a
tagged map containing the request id. The client matches the id against
a pending-callback registry.</li>
</ol>

<p>This is what nREPL provides via sessions and ops, just reinvented at the TCP
layer. It’s a fair amount of work for something nREPL gives you for free,
which only strengthens my view that nREPL is the better protocol for editor
tooling. Still, it was an interesting and educational exercise.</p>

<h2 id="zero-dependencies">Zero dependencies</h2>

<p>One thing I’m fairly proud of: Port has no hard dependencies. You’ll want either
<code class="language-plaintext highlighter-rouge">clojure-mode</code> or <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> installed for the source-buffer side of
things, but Port itself only soft-depends on them via runtime <code class="language-plaintext highlighter-rouge">fboundp</code>
checks. Hook it onto whichever one(s) you actually use. I intend to keep it that
way. Dependency creep is a real problem in the Emacs (every?) ecosystem, and a small
package should stay small.</p>

<h2 id="whats-there-in-01">What’s there in 0.1</h2>

<p>I tagged <a href="https://github.com/clojure-emacs/port/releases/tag/v0.1.0">v0.1.0</a>
yesterday. It’s small but already perfectly usable:</p>

<ul>
  <li><code class="language-plaintext highlighter-rouge">M-x port</code> “jacks in” (bootstraps) (auto-detects <code class="language-plaintext highlighter-rouge">deps.edn</code> / <code class="language-plaintext highlighter-rouge">project.clj</code> /
<code class="language-plaintext highlighter-rouge">bb.edn</code>, starts a server and connects to it)</li>
  <li>single-buffer REPL with persistent input history, completion, and eldoc
at the prompt</li>
  <li>interactive evaluation from source buffers with pretty-printed results</li>
  <li>structured stacktrace buffer with cause chain and navigable frames</li>
  <li><code class="language-plaintext highlighter-rouge">M-.</code> find-definition that follows into jar sources</li>
  <li>doc/source/apropos/macroexpand helpers</li>
</ul>

<p>MELPA submission is queued up next. After that, expect Port to be in
<a href="/articles/2025/11/16/burst-driven-development-my-approach-to-oss-projects-maintenance/">burst-driven</a>
maintenance mode like most of my smaller projects.</p>

<p>Feedback, ideas, and contributions are most welcome. The
<a href="https://github.com/clojure-emacs/port/issues">issue tracker</a> is the right
place.</p>

<h2 id="closing-thoughts">Closing thoughts</h2>

<p>Funny thing, I’d never actually written any code against prepl until I
started this project. It was fun to spend some quality time with the
“competition” of my beloved nREPL. Working with a different protocol always
teaches you something about the one you’re used to, and I came away from
this with a renewed appreciation for both: prepl is genuinely elegant for
what it is, and nREPL is genuinely well-designed for what we use it for.</p>

<p>Big thanks to <a href="https://www.clojuriststogether.org/">Clojurists Together</a> and
everyone else who supports my OSS Clojure work. You rock! Now if you’ll
excuse me, I have new releases of CIDER, clj-refactor, and refactor-nrepl
to get back to.</p>

<p>Keep hacking!</p>]]></content><author><name>Bozhidar Batsov</name></author><category term="Clojure" /><category term="Emacs" /><category term="Port" /><category term="prepl" /><summary type="html"><![CDATA[For ages I’ve had “add prepl support to CIDER” sitting somewhere in the back of my head. CIDER is built firmly around nREPL, but prepl ships with Clojure itself, and the appeal of dropping the external REPL server requirement is obvious. Recently, as part of a broader internals cleanup “mini-project” in CIDER, I finally sat down and put a prototype together: cider#3899.]]></summary></entry><entry><title type="html">Building Emacs Major Modes with Tree-sitter: Lessons Learned</title><link href="https://batsov.com/articles/2026/02/27/building-emacs-major-modes-with-treesitter-lessons-learned/" rel="alternate" type="text/html" title="Building Emacs Major Modes with Tree-sitter: Lessons Learned" /><published>2026-02-27T10:00:00+02:00</published><updated>2026-02-27T10:00:00+02:00</updated><id>https://batsov.com/articles/2026/02/27/building-emacs-major-modes-with-treesitter-lessons-learned</id><content type="html" xml:base="https://batsov.com/articles/2026/02/27/building-emacs-major-modes-with-treesitter-lessons-learned/"><![CDATA[<p>Over the past year I’ve been spending a lot of time building <a href="https://tree-sitter.github.io/tree-sitter/">Tree-sitter</a>-powered
major modes for Emacs – <a href="https://github.com/clojure-emacs/clojure-ts-mode">clojure-ts-mode</a>
(as co-maintainer), <a href="https://github.com/bbatsov/neocaml">neocaml</a> (from scratch),
and <a href="https://github.com/bbatsov/asciidoc-mode">asciidoc-mode</a> (also from scratch).
Between the three projects I’ve accumulated enough knowledge (and battle scars) to write about the
experience. This post distills the key lessons for anyone thinking about writing
a Tree-sitter-based major mode, or curious about what it’s actually like.</p>

<!--more-->

<h2 id="why-tree-sitter">Why Tree-sitter?</h2>

<p>Before Tree-sitter, Emacs font-locking was done with regular expressions and
indentation was handled by ad-hoc engines (SMIE, custom indent functions, or
pure regex heuristics). This works, but it has well-known problems:</p>

<ul>
  <li>
    <p><strong>Regex-based font-locking is fragile.</strong> Regexes can’t parse nested structures,
so they either under-match (missing valid code) or over-match (highlighting
inside strings and comments). Every edge case is another regex, and the patterns
become increasingly unreadable over time.</p>
  </li>
  <li>
    <p><strong>Indentation engines are complex.</strong> SMIE (the generic indentation engine for non-Tree-sitter modes) requires defining operator
precedence grammars for the language, which is hard to get right. Custom
indentation functions tend to grow into large, brittle state machines. Tuareg’s
indentation code, for example, is thousands of lines long.</p>
  </li>
</ul>

<p>Tree-sitter changes the game because you get a <strong>full, incremental, error-tolerant
syntax tree</strong> for free. Font-locking becomes “match this AST pattern, apply this
face”:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="c1">;; Highlight let-bound functions: match a let_binding with parameters</span>
<span class="p">(</span><span class="nv">let_binding</span> <span class="nv">pattern:</span> <span class="p">(</span><span class="nv">value_name</span><span class="p">)</span> <span class="nv">@font-lock-function-name-face</span>
             <span class="p">(</span><span class="nv">parameter</span><span class="p">)</span><span class="nb">+</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And indentation becomes “if the parent node is X, indent by Y”:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="c1">;; Children of a let_binding are indented by neocaml-indent-offset</span>
<span class="p">((</span><span class="nv">parent-is</span> <span class="s">"let_binding"</span><span class="p">)</span> <span class="nv">parent-bol</span> <span class="nv">neocaml-indent-offset</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>The rules are declarative, composable, and much easier to reason about than
regex chains.</p>

<p>In practice, <code class="language-plaintext highlighter-rouge">neocaml</code>’s entire font-lock and indentation logic fits in about 350
lines of Elisp. The equivalent in tuareg is spread across thousands of lines.
That’s the real selling point: <strong>simpler, more maintainable code that handles more
edge cases correctly</strong>.</p>

<h2 id="challenges">Challenges</h2>

<p>That said, Tree-sitter in Emacs is not a silver bullet. Here’s what I ran into.</p>

<h3 id="every-grammar-is-different">Every grammar is different</h3>

<p>Tree-sitter grammars are written by different authors with different philosophies.
The <a href="https://github.com/tree-sitter/tree-sitter-ocaml">tree-sitter-ocaml</a>
grammar provides a rich, detailed AST with named fields. The
<a href="https://github.com/sogaiu/tree-sitter-clojure">tree-sitter-clojure</a> grammar,
by contrast, deliberately keeps things minimal – it only models syntax, not
semantics, because Clojure’s macro system makes static semantic analysis
unreliable.<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup> This means font-locking <code class="language-plaintext highlighter-rouge">def</code> forms in Clojure requires
predicate matching on symbol text, while in OCaml you can directly match
<code class="language-plaintext highlighter-rouge">let_binding</code> nodes with named fields.</p>

<p>To illustrate: here’s how you’d fontify a function definition in OCaml, where
the grammar gives you rich named fields:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="c1">;; OCaml: grammar provides named fields -- direct structural match</span>
<span class="p">(</span><span class="nv">let_binding</span> <span class="nv">pattern:</span> <span class="p">(</span><span class="nv">value_name</span><span class="p">)</span> <span class="nv">@font-lock-function-name-face</span>
             <span class="p">(</span><span class="nv">parameter</span><span class="p">)</span><span class="nb">+</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And here’s the equivalent in Clojure, where the grammar only gives you lists of
symbols and you need predicate matching:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="c1">;; Clojure: grammar is syntax-only -- match by symbol text</span>
<span class="p">((</span><span class="nv">list_lit</span> <span class="ss">:anchor</span> <span class="p">(</span><span class="nv">sym_lit</span> <span class="nv">!namespace</span>
                            <span class="nv">name:</span> <span class="p">(</span><span class="nv">sym_name</span><span class="p">)</span> <span class="nv">@font-lock-keyword-face</span><span class="p">))</span>
 <span class="p">(</span><span class="ss">:match</span> <span class="o">,</span><span class="nv">clojure-ts--definition-keyword-regexp</span> <span class="nv">@font-lock-keyword-face</span><span class="p">))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>You can’t learn “how to write Tree-sitter queries” generically – you need to
learn each grammar individually. The best tool for this is <code class="language-plaintext highlighter-rouge">treesit-explore-mode</code>
(to visualize the full parse tree) and <code class="language-plaintext highlighter-rouge">treesit-inspect-mode</code> (to see the node
at point). Use them constantly.</p>

<h3 id="grammar-quality-varies-wildly">Grammar quality varies wildly</h3>

<p>You’re dependent on someone else providing the grammar, and quality is all over
the map. The OCaml grammar is mature and well-maintained – it’s hosted under the
official <a href="https://github.com/tree-sitter">tree-sitter</a> GitHub org. The Clojure
grammar is small and stable by design. But not every language is so lucky.</p>

<p><a href="https://github.com/bbatsov/asciidoc-mode">asciidoc-mode</a> uses a
<a href="https://github.com/cathaysia/tree-sitter-asciidoc">third-party AsciiDoc grammar</a>
that employs a dual-parser architecture – one parser for block-level structure
(headings, lists, code blocks) and another for inline formatting (bold, italic,
links). This is the same approach used by Emacs’s built-in <code class="language-plaintext highlighter-rouge">markdown-ts-mode</code>,
and it makes sense for markup languages where block and inline syntax are largely
independent.</p>

<p>The problem is that the two parsers run independently on the same text, and they
can <strong>disagree</strong>. The inline parser misinterprets <code class="language-plaintext highlighter-rouge">*</code> and <code class="language-plaintext highlighter-rouge">**</code> list markers as
emphasis delimiters, creating spurious bold spans that swallow subsequent inline
content. The workaround is to use <code class="language-plaintext highlighter-rouge">:override t</code> on all block-level font-lock
rules so they win over the incorrect inline faces:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="c1">;; Block-level rules use :override t so block-level faces win over</span>
<span class="c1">;; spurious inline emphasis nodes (the inline parser misreads `*'</span>
<span class="c1">;; list markers as emphasis delimiters).</span>
<span class="ss">:language</span> <span class="ss">'asciidoc</span>
<span class="ss">:override</span> <span class="no">t</span>
<span class="ss">:feature</span> <span class="ss">'list</span>
<span class="o">'</span><span class="p">((</span><span class="nv">ordered_list_marker</span><span class="p">)</span> <span class="nv">@font-lock-constant-face</span>
  <span class="p">(</span><span class="nv">unordered_list_marker</span><span class="p">)</span> <span class="nv">@font-lock-constant-face</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This doesn’t fix inline elements consumed by the spurious emphasis – that
requires an upstream grammar fix. When you hit grammar-level issues like this,
you either fix them yourself (which means diving into the grammar’s JavaScript
source and C toolchain) or you live with workarounds. Either way, it’s a
reminder that your mode is only as good as the grammar underneath it.</p>

<p>Getting the font-locking right in <code class="language-plaintext highlighter-rouge">asciidoc-mode</code> was probably the most
challenging part of all three projects, precisely because of these grammar
quirks. I also ran into a subtle <code class="language-plaintext highlighter-rouge">treesit</code> behavior: the default font-lock mode
(<code class="language-plaintext highlighter-rouge">:override nil</code>) skips an entire captured range if <em>any</em> position within it
already has a face. So if you capture a parent node like <code class="language-plaintext highlighter-rouge">(inline_macro)</code> and a
child was already fontified, the whole thing gets skipped silently. The fix is
to capture specific child nodes instead:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="c1">;; BAD: entire node gets skipped if any child is already fontified</span>
<span class="c1">;; (inline_macro) @font-lock-function-call-face</span>

<span class="c1">;; GOOD: capture specific children</span>
<span class="p">(</span><span class="nv">inline_macro</span> <span class="p">(</span><span class="nv">macro_name</span><span class="p">)</span> <span class="nv">@font-lock-function-call-face</span><span class="p">)</span>
<span class="p">(</span><span class="nv">inline_macro</span> <span class="p">(</span><span class="nv">target</span><span class="p">)</span> <span class="nv">@font-lock-string-face</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>These issues took a lot of trial and error to diagnose. The lesson: <strong>budget
extra time for font-locking when working with less mature grammars</strong>.</p>

<h3 id="grammar-versions-and-breaking-changes">Grammar versions and breaking changes</h3>

<p>Grammars evolve, and breaking changes happen. <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> switched from
the stable grammar to the <a href="https://github.com/sogaiu/tree-sitter-clojure/tree/unstable-20250526">experimental
branch</a>
because the stable version had metadata nodes as children of other nodes, which
caused <code class="language-plaintext highlighter-rouge">forward-sexp</code> and <code class="language-plaintext highlighter-rouge">kill-sexp</code> to behave incorrectly. The experimental
grammar makes metadata standalone nodes, fixing the navigation issues but
requiring all queries to be updated.</p>

<p><code class="language-plaintext highlighter-rouge">neocaml</code> pins to
<a href="https://github.com/tree-sitter/tree-sitter-ocaml/tree/v0.24.0">v0.24.0</a> of the
OCaml grammar. If you don’t pin versions, a grammar update can silently break
your font-locking or indentation.</p>

<p>The takeaway: <strong>always pin your grammar version</strong>, and include a mechanism to
detect outdated grammars. <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> tests a query that changed between
versions to detect incompatible grammars at startup.</p>

<h3 id="grammar-delivery">Grammar delivery</h3>

<p>Users shouldn’t have to manually clone repos and compile C code to use your
mode. Both <code class="language-plaintext highlighter-rouge">neocaml</code> and <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> include grammar recipes:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nv">defconst</span> <span class="nv">neocaml-grammar-recipes</span>
  <span class="o">'</span><span class="p">((</span><span class="nv">ocaml</span> <span class="s">"https://github.com/tree-sitter/tree-sitter-ocaml"</span>
           <span class="s">"v0.24.0"</span>
           <span class="s">"grammars/ocaml/src"</span><span class="p">)</span>
    <span class="p">(</span><span class="nv">ocaml-interface</span> <span class="s">"https://github.com/tree-sitter/tree-sitter-ocaml"</span>
                     <span class="s">"v0.24.0"</span>
                     <span class="s">"grammars/interface/src"</span><span class="p">)))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>On first use, the mode checks <code class="language-plaintext highlighter-rouge">treesit-language-available-p</code> and offers to install
missing grammars via <code class="language-plaintext highlighter-rouge">treesit-install-language-grammar</code>. This works, but requires
a C compiler and Git on the user’s machine, which is not ideal.<sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup></p>

<h3 id="the-emacs-tree-sitter-apis-are-a-moving-target">The Emacs Tree-sitter APIs are a moving target</h3>

<p>The Tree-sitter support in Emacs has been improving steadily, but each version
has its quirks:</p>

<p><strong>Emacs 29</strong> introduced Tree-sitter support but lacked several APIs. For instance,
<code class="language-plaintext highlighter-rouge">treesit-thing-settings</code> (used for structured navigation) doesn’t exist – you
need a fallback:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="c1">;; Fallback for Emacs 29 (no treesit-thing-settings)</span>
<span class="p">(</span><span class="nb">unless</span> <span class="p">(</span><span class="nb">boundp</span> <span class="ss">'treesit-thing-settings</span><span class="p">)</span>
  <span class="p">(</span><span class="nv">setq-local</span> <span class="nv">forward-sexp-function</span> <span class="nf">#'</span><span class="nv">neocaml-forward-sexp</span><span class="p">))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Emacs 30</strong> added <code class="language-plaintext highlighter-rouge">treesit-thing-settings</code>, sentence navigation, and better
indentation support. But it also had a bug in <code class="language-plaintext highlighter-rouge">treesit-range-settings</code> offsets
(<a href="https://debbugs.gnu.org/cgi/bugreport.cgi?bug=77848">#77848</a>) that broke
embedded parsers, and another in <code class="language-plaintext highlighter-rouge">treesit-transpose-sexps</code> that required
<code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> to disable its Tree-sitter-aware version.</p>

<p><strong>Emacs 31</strong> has a bug in <code class="language-plaintext highlighter-rouge">treesit-forward-comment</code> where an off-by-one error
causes <code class="language-plaintext highlighter-rouge">uncomment-region</code> to leave <code class="language-plaintext highlighter-rouge">*)</code> behind on multi-line OCaml comments. I
had to skip the affected test with a version check:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nb">when</span> <span class="p">(</span><span class="nb">&gt;=</span> <span class="nv">emacs-major-version</span> <span class="mi">31</span><span class="p">)</span>
  <span class="p">(</span><span class="nb">signal</span> <span class="ss">'buttercup-pending</span>
          <span class="s">"Emacs 31 treesit-forward-comment bug (off-by-one)"</span><span class="p">))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>The lesson: <strong>test your mode against multiple Emacs versions</strong>, and be prepared
to write version-specific workarounds. CI that runs against Emacs 29, 30, and
snapshot is essential.</p>

<h3 id="no-scm-file-support-yet">No .scm file support (yet)</h3>

<p>Most Tree-sitter grammars ship with <code class="language-plaintext highlighter-rouge">.scm</code> query files for syntax highlighting
(<code class="language-plaintext highlighter-rouge">highlights.scm</code>) and indentation (<code class="language-plaintext highlighter-rouge">indents.scm</code>). Editors like Neovim and
Helix use these directly. Emacs doesn’t – you have to manually translate the
<code class="language-plaintext highlighter-rouge">.scm</code> patterns into <code class="language-plaintext highlighter-rouge">treesit-font-lock-rules</code> and <code class="language-plaintext highlighter-rouge">treesit-simple-indent-rules</code>
calls in Elisp.</p>

<p>This is tedious and error-prone. For example, here’s a rule from the OCaml
grammar’s <code class="language-plaintext highlighter-rouge">highlights.scm</code>:</p>

<div class="language-scheme highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="c1">;; upstream .scm (used by Neovim, Helix, etc.)</span>
<span class="p">(</span><span class="nf">constructor_name</span><span class="p">)</span> <span class="nv">@type</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And here’s the Elisp equivalent you’d write for Emacs:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="c1">;; Emacs equivalent -- wrapped in treesit-font-lock-rules</span>
<span class="ss">:language</span> <span class="ss">'ocaml</span>
<span class="ss">:feature</span> <span class="ss">'type</span>
<span class="o">'</span><span class="p">((</span><span class="nv">constructor_name</span><span class="p">)</span> <span class="nv">@font-lock-type-face</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>The query syntax is nearly identical, but you have to wrap everything in
<code class="language-plaintext highlighter-rouge">treesit-font-lock-rules</code> calls, map upstream capture names (<code class="language-plaintext highlighter-rouge">@type</code>) to Emacs
face names (<code class="language-plaintext highlighter-rouge">@font-lock-type-face</code>), assign features, and manage <code class="language-plaintext highlighter-rouge">:override</code>
behavior. You end up maintaining a parallel set of queries that can drift from
upstream. Emacs 31 will introduce
<a href="https://github.com/emacs-mirror/emacs/blob/master/lisp/treesit-x.el#L47"><code class="language-plaintext highlighter-rouge">define-treesit-generic-mode</code></a>
which will make it possible to use <code class="language-plaintext highlighter-rouge">.scm</code> files for font-locking, which should
help significantly. But for now, you’re hand-coding everything.</p>

<h2 id="tips-and-tricks">Tips and tricks</h2>

<h3 id="debugging-font-locking">Debugging font-locking</h3>

<p>When a face isn’t being applied where you expect:</p>

<ol>
  <li>Use <code class="language-plaintext highlighter-rouge">treesit-inspect-mode</code> to verify the node type at point matches your
query.</li>
  <li>Set <code class="language-plaintext highlighter-rouge">treesit--font-lock-verbose</code> to <code class="language-plaintext highlighter-rouge">t</code> to see which rules are firing.</li>
  <li>Check the font-lock feature level – your rule might be in level 4 while the
user has the default level 3. The features are assigned to levels via
<code class="language-plaintext highlighter-rouge">treesit-font-lock-feature-list</code>.</li>
  <li>Remember that <strong>rule order matters</strong>. Without <code class="language-plaintext highlighter-rouge">:override</code>, an earlier rule that
already fontified a region will prevent later rules from applying. This can be
intentional (e.g. builtin types at level 3 take precedence over generic types)
or a source of bugs.</li>
</ol>

<h3 id="use-the-font-lock-levels-wisely">Use the font-lock levels wisely</h3>

<p>Tree-sitter modes define four levels of font-locking via
<code class="language-plaintext highlighter-rouge">treesit-font-lock-feature-list</code>, and the default level in Emacs is 3. It’s
tempting to pile everything into levels 1–3 so users see maximum highlighting
out of the box, but resist the urge. When every token on the screen has a
different color, code starts looking like a Christmas tree and the important
things – keywords, definitions, types – stop standing out.</p>

<p>Less is more here. Here’s how <code class="language-plaintext highlighter-rouge">neocaml</code> distributes features across levels:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nv">setq-local</span> <span class="nv">treesit-font-lock-feature-list</span>
            <span class="o">'</span><span class="p">((</span><span class="nv">comment</span> <span class="nv">definition</span><span class="p">)</span>
              <span class="p">(</span><span class="kt">keyword</span> <span class="nb">string</span> <span class="nc">number</span><span class="p">)</span>
              <span class="p">(</span><span class="nv">attribute</span> <span class="nv">builtin</span> <span class="nv">constant</span> <span class="k">type</span><span class="p">)</span>
              <span class="p">(</span><span class="nv">operator</span> <span class="nv">bracket</span> <span class="nv">delimiter</span> <span class="nv">variable</span> <span class="k">function</span><span class="p">)))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> follows the same philosophy:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nv">setq-local</span> <span class="nv">treesit-font-lock-feature-list</span>
            <span class="o">'</span><span class="p">((</span><span class="nv">comment</span> <span class="nv">definition</span><span class="p">)</span>
              <span class="p">(</span><span class="kt">keyword</span> <span class="nb">string</span> <span class="nb">char</span> <span class="nc">symbol</span> <span class="nv">builtin</span> <span class="k">type</span><span class="p">)</span>
              <span class="p">(</span><span class="nv">constant</span> <span class="nc">number</span> <span class="k">quote</span> <span class="nv">metadata</span> <span class="nv">doc</span> <span class="nv">regex</span><span class="p">)</span>
              <span class="p">(</span><span class="nv">bracket</span> <span class="nv">deref</span> <span class="k">function</span> <span class="nv">tagged-literals</span><span class="p">)))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>The pattern is the same: essentials first, progressively more detail at higher
levels. This way the default experience (level 3) is clean and readable, and
users who want the full rainbow can bump <code class="language-plaintext highlighter-rouge">treesit-font-lock-level</code> to 4. Better
yet, they can use <code class="language-plaintext highlighter-rouge">treesit-font-lock-recompute-features</code> to cherry-pick
individual features regardless of level:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="c1">;; Enable 'function' (level 4) without enabling all of level 4</span>
<span class="p">(</span><span class="nv">treesit-font-lock-recompute-features</span> <span class="o">'</span><span class="p">(</span><span class="k">function</span><span class="p">)</span> <span class="no">nil</span><span class="p">)</span>

<span class="c1">;; Disable 'bracket' even if the user's level would include it</span>
<span class="p">(</span><span class="nv">treesit-font-lock-recompute-features</span> <span class="no">nil</span> <span class="o">'</span><span class="p">(</span><span class="nv">bracket</span><span class="p">))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This gives users fine-grained control without requiring mode authors to
anticipate every preference.</p>

<h3 id="debugging-indentation">Debugging indentation</h3>

<p>Indentation issues are harder to diagnose because they depend on tree structure,
rule ordering, and anchor resolution:</p>

<ol>
  <li>Set <code class="language-plaintext highlighter-rouge">treesit--indent-verbose</code> to <code class="language-plaintext highlighter-rouge">t</code> – this logs which rule matched for each
line, what anchor was computed, and the final column.</li>
  <li>Use <code class="language-plaintext highlighter-rouge">treesit-explore-mode</code> to understand the parent chain. The key question
is always: “what is the parent node, and which rule matches it?”</li>
  <li>
    <p>Remember that <strong>rule order matters</strong> for indentation too – the first matching
rule wins. A typical set of rules reads top to bottom from most specific to
most general:</p>

    <div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="rouge-code"><pre><span class="c1">;; Closing delimiters align with the opening construct</span>
<span class="p">((</span><span class="nv">node-is</span> <span class="s">")"</span><span class="p">)</span> <span class="nv">parent-bol</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">((</span><span class="nv">node-is</span> <span class="s">"end"</span><span class="p">)</span> <span class="nv">parent-bol</span> <span class="mi">0</span><span class="p">)</span>

<span class="c1">;; then/else clauses align with their enclosing if</span>
<span class="p">((</span><span class="nv">node-is</span> <span class="s">"then_clause"</span><span class="p">)</span> <span class="nv">parent-bol</span> <span class="mi">0</span><span class="p">)</span>
<span class="p">((</span><span class="nv">node-is</span> <span class="s">"else_clause"</span><span class="p">)</span> <span class="nv">parent-bol</span> <span class="mi">0</span><span class="p">)</span>

<span class="c1">;; Bodies inside then/else are indented</span>
<span class="p">((</span><span class="nv">parent-is</span> <span class="s">"then_clause"</span><span class="p">)</span> <span class="nv">parent-bol</span> <span class="nv">neocaml-indent-offset</span><span class="p">)</span>
<span class="p">((</span><span class="nv">parent-is</span> <span class="s">"else_clause"</span><span class="p">)</span> <span class="nv">parent-bol</span> <span class="nv">neocaml-indent-offset</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div>    </div>
  </li>
  <li>
    <p>Watch out for the <strong>empty-line problem</strong>: when the cursor is on a blank line,
Tree-sitter has no node at point. The indentation engine falls back to the root
<code class="language-plaintext highlighter-rouge">compilation_unit</code> node as the parent, which typically matches the top-level
rule and gives column 0. In neocaml I solved this with a <code class="language-plaintext highlighter-rouge">no-node</code> rule that
looks at the previous line’s last token to decide indentation:</p>

    <div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nv">no-node</span> <span class="nv">prev-line</span> <span class="nv">neocaml--empty-line-offset</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div>    </div>
  </li>
</ol>

<h3 id="build-a-comprehensive-test-suite">Build a comprehensive test suite</h3>

<p>This is the single most important piece of advice. Font-lock and indentation are
easy to break accidentally, and manual testing doesn’t scale. Both projects use
<a href="https://github.com/jorgenschaefer/emacs-buttercup">Buttercup</a> (a BDD testing
framework for Emacs) with custom test macros.</p>

<p><strong>Font-lock tests</strong> insert code into a buffer, run <code class="language-plaintext highlighter-rouge">font-lock-ensure</code>, and assert
that specific character ranges have the expected face:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nv">when-fontifying-it</span> <span class="s">"fontifies let-bound functions"</span>
  <span class="p">(</span><span class="s">"let greet name = ..."</span>
   <span class="p">(</span><span class="mi">5</span> <span class="mi">9</span> <span class="nv">font-lock-function-name-face</span><span class="p">)))</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Indentation tests</strong> insert code, run <code class="language-plaintext highlighter-rouge">indent-region</code>, and assert the result
matches the expected indentation:</p>

<div class="language-emacs-lisp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nv">when-indenting-it</span> <span class="s">"indents a match expression"</span>
  <span class="s">"match x with"</span>
  <span class="s">"| 0 -&gt; \"zero\""</span>
  <span class="s">"| n -&gt; string_of_int n"</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><strong>Integration tests</strong> load real source files and verify that both font-locking
and indentation survive <code class="language-plaintext highlighter-rouge">indent-region</code> on the full file. This catches
interactions between rules that unit tests miss.</p>

<p><code class="language-plaintext highlighter-rouge">neocaml</code> has 200+ automated tests and <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> has even more.
Investing in test infrastructure early pays off enormously – I can refactor
indentation rules with confidence because the suite catches regressions
immediately.</p>

<h4 id="a-personal-story-on-testing-roi">A personal story on testing ROI</h4>

<p>When I became the maintainer of
<a href="https://github.com/clojure-emacs/clojure-mode">clojure-mode</a> many years ago, I
really struggled with making changes. There were no font-lock or indentation
tests, so every change was a leap of faith – you’d fix one thing and break three
others without knowing until someone filed a bug report. I spent years working
on a testing approach I was happy with, alongside many great contributors, and
the return on investment was massive.</p>

<p>The same approach – almost the same test macros – carried over directly to
<code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> when we built the Tree-sitter version. And later I reused the
pattern again in <code class="language-plaintext highlighter-rouge">neocaml</code> and <code class="language-plaintext highlighter-rouge">asciidoc-mode</code>. One investment in testing
infrastructure, four projects benefiting from it.</p>

<p>I know that automated tests, for whatever reason, never gained much traction in
the Emacs community. Many popular packages have no tests at all. I hope stories
like this convince you that investing in tests is really important and pays off
– not just for the project where you write them, but for every project you build
after.</p>

<h3 id="pre-compile-queries">Pre-compile queries</h3>

<p>This one is specific to <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> but applies broadly: compiling
Tree-sitter queries at runtime is expensive. If you’re building queries
dynamically (e.g. with <code class="language-plaintext highlighter-rouge">treesit-font-lock-rules</code> called at mode init time),
consider pre-compiling them as <code class="language-plaintext highlighter-rouge">defconst</code> values. This made a noticeable
difference in <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code>’s startup time.</p>

<h2 id="a-note-on-naming">A note on naming</h2>

<p>The Emacs community has settled on a <code class="language-plaintext highlighter-rouge">-ts-mode</code> suffix convention for
Tree-sitter-based modes: <code class="language-plaintext highlighter-rouge">python-ts-mode</code>, <code class="language-plaintext highlighter-rouge">c-ts-mode</code>, <code class="language-plaintext highlighter-rouge">ruby-ts-mode</code>, and so
on. This makes sense when both a legacy mode and a Tree-sitter mode coexist in
Emacs core – users need to choose between them. But I think the convention is
being applied too broadly, and I’m afraid the resulting name fragmentation will
haunt the community for years.</p>

<p>For new packages that don’t have a legacy counterpart, the <code class="language-plaintext highlighter-rouge">-ts-mode</code> suffix is
unnecessary. I named my packages <code class="language-plaintext highlighter-rouge">neocaml</code> (not <code class="language-plaintext highlighter-rouge">ocaml-ts-mode</code>) and
<code class="language-plaintext highlighter-rouge">asciidoc-mode</code> (not <code class="language-plaintext highlighter-rouge">adoc-ts-mode</code>) because there was no prior <code class="language-plaintext highlighter-rouge">neocaml-mode</code>
or <code class="language-plaintext highlighter-rouge">asciidoc-mode</code> to disambiguate from. The <code class="language-plaintext highlighter-rouge">-ts-</code> infix is an implementation
detail that shouldn’t leak into the user-facing name. Will we rename everything
again when Tree-sitter becomes the default and the non-TS variants are removed?</p>

<p>Be bolder with naming. If you’re building something new, give it a name that
makes sense on its own merits, not one that encodes the parsing technology in the
package name.</p>

<h2 id="the-road-ahead">The road ahead</h2>

<p>I think the full transition to Tree-sitter in the Emacs community will take
3–5 years, optimistically. There are hundreds of major modes out there, many
maintained by a single person in their spare time. Converting a mode from regex
to Tree-sitter isn’t just a mechanical translation – you need to understand the
grammar, rewrite font-lock and indentation rules, handle version compatibility,
and build a new test suite. That’s a lot of work.</p>

<p>Interestingly, this might be one area where agentic coding tools can genuinely
help. The structure of Tree-sitter-based major modes is fairly uniform: grammar
recipes, font-lock rules, indentation rules, navigation settings, imenu. If you
give an AI agent a grammar and a reference to a high-quality mode like
<code class="language-plaintext highlighter-rouge">clojure-ts-mode</code>, it could probably scaffold a reasonable new mode fairly
quickly. The hard parts – debugging grammar quirks, handling edge cases, getting
indentation <em>just right</em> – would still need human attention, but the boilerplate
could be automated.</p>

<p>Still, knowing the Emacs community, I wouldn’t be surprised if a full migration
never actually completes. Many old-school modes work perfectly fine, their
maintainers have no interest in Tree-sitter, and “if it ain’t broke, don’t fix
it” is a powerful force. And that’s okay – diversity of approaches is part of
what makes Emacs Emacs.</p>

<h2 id="closing-thoughts">Closing thoughts</h2>

<p>Tree-sitter is genuinely great for building Emacs major modes. The code is
simpler, the results are more accurate, and incremental parsing means everything
stays fast even on large files. I wouldn’t go back to regex-based font-locking
willingly.</p>

<p>But it’s not magical. Grammars are inconsistent across languages, the Emacs APIs
are still maturing, you can’t reuse <code class="language-plaintext highlighter-rouge">.scm</code> files (yet), and you’ll hit
version-specific bugs that require tedious workarounds. The testing story is
better than with regex modes – tree structures are more predictable than regex
matches – but you still need a solid test suite to avoid regressions.</p>

<p>If you’re thinking about writing a Tree-sitter-based major mode, do it. The
ecosystem needs more of them, and the experience of working with syntax trees
instead of regexes is genuinely enjoyable. Just go in with realistic
expectations, pin your grammar versions, test against multiple Emacs releases,
and build your test suite early.</p>

<p>Anyways, I wish there was an article like this one when I was starting out
with <code class="language-plaintext highlighter-rouge">clojure-ts-mode</code> and <code class="language-plaintext highlighter-rouge">neocaml</code>, so there you have it. I hope that
the lessons I’ve learned along the way will help build better modes
with Tree-sitter down the road.</p>

<p>That’s all I have for you today. Keep hacking!</p>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>See the excellent <a href="https://github.com/sogaiu/tree-sitter-clojure/blob/master/doc/scope.md">scope discussion</a> in the tree-sitter-clojure repo for the rationale. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
    <li id="fn:2">
      <p>There’s ongoing discussion in the Emacs community about distributing pre-compiled grammar binaries, but nothing concrete yet. <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Bozhidar Batsov</name></author><category term="Emacs" /><category term="OCaml" /><category term="Clojure" /><category term="AsciiDoc" /><summary type="html"><![CDATA[Over the past year I’ve been spending a lot of time building Tree-sitter-powered major modes for Emacs – clojure-ts-mode (as co-maintainer), neocaml (from scratch), and asciidoc-mode (also from scratch). Between the three projects I’ve accumulated enough knowledge (and battle scars) to write about the experience. This post distills the key lessons for anyone thinking about writing a Tree-sitter-based major mode, or curious about what it’s actually like.]]></summary></entry><entry><title type="html">OCaml at First Glance</title><link href="https://batsov.com/articles/2022/08/29/ocaml-at-first-glance/" rel="alternate" type="text/html" title="OCaml at First Glance" /><published>2022-08-29T07:31:00+03:00</published><updated>2022-08-31T00:00:00+03:00</updated><id>https://batsov.com/articles/2022/08/29/ocaml-at-first-glance</id><content type="html" xml:base="https://batsov.com/articles/2022/08/29/ocaml-at-first-glance/"><![CDATA[<blockquote>
  <p>You never get a second chance to make a first impression.</p>
</blockquote>

<p>Lately I’ve been <a href="/articles/2022/08/19/learning-ocaml/">learning OCaml</a> and
I thought it might be a good time to share a few initial impressions, while they
are still fresh. Of course, you should take those with a grain of salt, given
that my knowledge of OCaml is still quite basic and I might be missing out
on some stuff.</p>

<p>Also you should keep in mind my own background, experiences and preferences:</p>

<ul>
  <li>Over the course of almost 20 years I’ve been programming professionally (mostly) in C, C++, Java, Scala and Ruby. The last 10 years have been almost exclusively dedicated to Ruby.</li>
  <li>I’ve always been super passionate about programming languages and on the side I’ve learned to some extent about a dozen more languages (e.g. several Lisps, Haskell, Erlang, etc)</li>
  <li>I’ve got a massive fondness for the simplicity of Lisps, I’ve written a ton of code in Emacs Lisp and Clojure, and I really love REPL-driven (interactive/incremental) programming.</li>
  <li>I’ve never been a strong proponent of either dynamically or statically typed programming languages. I’ve yet to see a compelling argument in favor of one side or the other.</li>
</ul>

<p>Obviously, all of this tends to influence a lot my perception of (new) programming languages. That’s why throughout the article I’ll be drawing some comparisons to other (functional) programming languages that I’m familiar with.</p>

<h2 id="general-notes">General Notes</h2>

<blockquote>
  <p>OCaml is a general-purpose, industrial-strength programming language with an emphasis on expressiveness and safety.</p>

  <p>– ocaml.org</p>
</blockquote>

<p>The language strikes a good balance between being functional and practical. In this sense it really reminds me of Clojure on the practical side and Haskell on the somewhat impractical side (the pursuit of purity comes at a cost). OCaml’s strict evaluation model makes it easier to reason than Haskell and also results in programs with more predictable performance.</p>

<h2 id="language-syntax">Language Syntax</h2>

<p>Language syntax is a favorite bike-shedding topic for most programmers, so I’ll do my best to stay constructive here. I think OCaml’s syntax won’t present much of a challenge for any seasoned programmer, especially for someone with a passing knowledge of a similar language like Haskell.</p>

<p>To put the discussion that follows in context, here are a few trivial OCaml code snippets:</p>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="n">square</span> <span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span>

<span class="n">square</span> <span class="mi">5</span>

<span class="c">(* a classic function definition *)</span>
<span class="k">let</span> <span class="n">hello</span> <span class="n">name</span> <span class="o">=</span> <span class="n">print_endline</span> <span class="p">(</span><span class="s2">"Hello, "</span> <span class="o">^</span> <span class="n">name</span> <span class="o">^</span> <span class="s2">"!"</span><span class="p">)</span>

<span class="n">hello</span> <span class="s2">"world"</span>

<span class="c">(* filter a list with some predicate *)</span>
<span class="k">let</span> <span class="n">is_even</span> <span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="ow">mod</span> <span class="mi">2</span> <span class="o">=</span> <span class="mi">0</span>

<span class="nn">List</span><span class="p">.</span><span class="n">filter</span> <span class="n">is_even</span> <span class="p">[</span><span class="mi">1</span><span class="p">;</span> <span class="mi">2</span><span class="p">;</span> <span class="mi">3</span><span class="p">;</span> <span class="mi">4</span><span class="p">;</span> <span class="mi">5</span><span class="p">;</span> <span class="mi">6</span><span class="p">;</span> <span class="mi">7</span><span class="p">;</span> <span class="mi">8</span><span class="p">;</span> <span class="mi">9</span><span class="p">]</span>
<span class="o">-</span> <span class="o">:</span> <span class="kt">int</span> <span class="kt">list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">;</span> <span class="mi">4</span><span class="p">;</span> <span class="mi">6</span><span class="p">;</span> <span class="mi">8</span><span class="p">]</span>

<span class="c">(* compute factorial recursively *)</span>
<span class="k">let</span> <span class="k">rec</span> <span class="n">fact</span> <span class="n">x</span> <span class="o">=</span>
  <span class="k">if</span> <span class="n">x</span> <span class="o">&lt;=</span> <span class="mi">1</span> <span class="k">then</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">x</span> <span class="o">*</span> <span class="n">fact</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>

<span class="n">fact</span> <span class="mi">10</span>

<span class="c">(* reverse a list using pattern matching and tail recursion *)</span>
<span class="k">let</span> <span class="n">rev</span> <span class="n">lst</span> <span class="o">=</span>
  <span class="k">let</span> <span class="k">rec</span> <span class="n">rev'</span> <span class="n">res</span> <span class="o">=</span> <span class="k">function</span>
  <span class="o">|</span> <span class="bp">[]</span> <span class="o">-&gt;</span> <span class="n">res</span>
  <span class="o">|</span> <span class="n">h</span> <span class="o">::</span> <span class="n">t</span> <span class="o">-&gt;</span> <span class="n">rev'</span> <span class="p">(</span><span class="n">h</span> <span class="o">::</span> <span class="n">res</span><span class="p">)</span> <span class="n">t</span>
  <span class="k">in</span>
  <span class="n">rev'</span> <span class="bp">[]</span> <span class="n">lst</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>I hope those snippets gave you some notion of OCaml’s syntax. I’ll admit
that I’m fairly fond of it by now, although it definitely takes a bit of time to
get used to it.</p>

<h3 id="things-i-liked">Things I Liked</h3>

<p>Some aspects of the OCaml syntax that I liked:</p>

<ul>
  <li>using <code class="language-plaintext highlighter-rouge">;;</code> as expression terminator - that’s useful in toplevels as it makes it easy to input a multi-line expression. It’s also optional elsewhere, so it doesn’t add unneeded verbosity.</li>
  <li>being explicit about recursive bindings. Notice the <code class="language-plaintext highlighter-rouge">rec</code> keyword here:</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="k">rec</span> <span class="n">last</span> <span class="o">=</span> <span class="k">function</span>
  <span class="o">|</span> <span class="bp">[]</span> <span class="o">-&gt;</span> <span class="nc">None</span>
  <span class="o">|</span> <span class="p">[</span> <span class="n">x</span> <span class="p">]</span> <span class="o">-&gt;</span> <span class="nc">Some</span> <span class="n">x</span>
  <span class="o">|</span> <span class="n">_</span> <span class="o">::</span> <span class="n">t</span> <span class="o">-&gt;</span> <span class="n">last</span> <span class="n">t</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>type signatures are optional</li>
  <li>module types</li>
  <li>the ability to restrict an open module to a very small scope</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="n">average</span> <span class="n">x</span> <span class="n">y</span> <span class="o">=</span>
  <span class="k">let</span> <span class="k">open</span> <span class="nc">Int64</span> <span class="k">in</span>
  <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span> <span class="o">/</span> <span class="n">of_int</span> <span class="mi">2</span>

<span class="k">let</span> <span class="n">average</span> <span class="n">x</span> <span class="n">y</span> <span class="o">=</span>
  <span class="nn">Int64</span><span class="p">.((</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)</span> <span class="o">/</span> <span class="n">of_int</span> <span class="mi">2</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>labeled arguments</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="n">div</span> <span class="o">~</span><span class="n">num</span> <span class="o">~</span><span class="n">denom</span> <span class="o">=</span> <span class="n">num</span> <span class="o">/</span> <span class="n">denom</span>

<span class="n">div</span> <span class="o">~</span><span class="n">num</span><span class="o">:</span><span class="mi">3</span> <span class="o">~</span><span class="n">denom</span><span class="o">:</span><span class="mi">10</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>the ability to place the signature of a module in a dedicated <code class="language-plaintext highlighter-rouge">.mli</code> file.</li>
  <li>the ability to have user-defined types that look as something built-in (e.g. lists, <code class="language-plaintext highlighter-rouge">ref</code>s, etc)</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="c">(* here's how you can define ref yourselves *)</span>
<span class="k">type</span> <span class="k">'</span><span class="n">a</span> <span class="n">ref</span> <span class="o">=</span> <span class="p">{</span> <span class="k">mutable</span> <span class="n">contents</span> <span class="o">:</span> <span class="k">'</span><span class="n">a</span> <span class="p">}</span>

<span class="k">let</span> <span class="n">ref</span> <span class="n">x</span> <span class="o">=</span> <span class="p">{</span> <span class="n">contents</span> <span class="o">=</span> <span class="n">x</span> <span class="p">}</span>

<span class="k">let</span> <span class="p">(</span><span class="o">!</span><span class="p">)</span> <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">contents</span>

<span class="k">let</span> <span class="p">(</span><span class="o">:=</span><span class="p">)</span> <span class="n">r</span> <span class="n">x</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">contents</span> <span class="o">&lt;-</span> <span class="n">x</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>having handy functions like <code class="language-plaintext highlighter-rouge">|&gt;</code> out-of-the-box (I’m quite used to this from Clojure)</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">some_list</span> <span class="o">|&gt;</span> <span class="nn">List</span><span class="p">.</span><span class="n">filter</span> <span class="n">is_even</span> <span class="o">|&gt;</span> <span class="n">sum</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>By the way, there’s nothing really special about <code class="language-plaintext highlighter-rouge">|&gt;</code>, it’s a function like any other. One of the nice aspects of OCaml’s syntax in general.</p>

<ul>
  <li>the usages of <code class="language-plaintext highlighter-rouge">snake_case</code> instead of <code class="language-plaintext highlighter-rouge">camelCase</code> most of the time. I find <code class="language-plaintext highlighter-rouge">snake_case</code> identifiers to be more readable, although obviously that’s super subjective.</li>
</ul>

<h3 id="things-i-disliked">Things I Disliked</h3>

<p>Some aspects of the syntax that I disliked or found confusing (at least initially):</p>

<ul>
  <li>using <code class="language-plaintext highlighter-rouge">(* *)</code> for comments and lack of line comments (e.g. <code class="language-plaintext highlighter-rouge">//</code> is some C-like
languages). Not the end of the world, but the comment syntax makes some things
trickier (e.g. referring to the multiplication operator <code class="language-plaintext highlighter-rouge">*</code>) and is just too
verbose.</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="c">(* this will work *)</span>
<span class="nn">List</span><span class="p">.</span><span class="n">fold_left</span> <span class="p">(</span><span class="o">+</span><span class="p">)</span> <span class="mi">0</span> <span class="p">[</span><span class="mi">1</span><span class="p">;</span><span class="mi">2</span><span class="p">;</span><span class="mi">3</span><span class="p">;</span><span class="mi">4</span><span class="p">;</span><span class="mi">5</span><span class="p">]</span>

<span class="c">(* this will work *)</span>
<span class="nn">List</span><span class="p">.</span><span class="n">fold_left</span> <span class="p">(</span> <span class="o">*</span> <span class="p">)</span> <span class="mi">1</span> <span class="p">[</span><span class="mi">1</span><span class="p">;</span><span class="mi">2</span><span class="p">;</span><span class="mi">3</span><span class="p">;</span><span class="mi">4</span><span class="p">;</span><span class="mi">5</span><span class="p">]</span>

<span class="c">(* this will not work *)</span>
<span class="nn">List</span><span class="p">.</span><span class="n">fold_left</span> <span class="p">(</span><span class="o">*</span><span class="p">)</span> <span class="mi">1</span> <span class="p">[</span><span class="mi">1</span><span class="p">;</span><span class="mi">2</span><span class="p">;</span><span class="mi">3</span><span class="p">;</span><span class="mi">4</span><span class="p">;</span><span class="mi">5</span><span class="p">]</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Here’s one more comment that won’t work - <code class="language-plaintext highlighter-rouge">(* " *)</code>. I’ll leave to the curious reader to figure out why that’s the case.</p>

<ul>
  <li>using <code class="language-plaintext highlighter-rouge">;</code> heavily as element separator (e.g. in lists and records):</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="n">some_list</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">;</span> <span class="mi">2</span><span class="p">;</span> <span class="mi">3</span><span class="p">;</span> <span class="mi">4</span><span class="p">;</span> <span class="mi">5</span><span class="p">]</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>You get used to this, but it’s a pretty big departure from the norm to use commas. I wonder what’s the reasoning behind this. Most likely it has to do with being able to define tuples without parentheses around them:</p>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="p">);;</span>
<span class="c">(* - : int * int * int = (1, 2, 3) *)</span>

<span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="p">;;</span>
<span class="c">(* - : int * int * int = (1, 2, 3) *)</span>

<span class="c">(* a more realistic example of the usefulness of this *)</span>
<span class="k">let</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">,</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span> <span class="k">in</span>
<span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span>

<span class="c">(* but we pay a steep price for the above convenience *)</span>
<span class="p">[</span><span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="mi">3</span><span class="p">];;</span>
<span class="c">(* - : (int * int * int) list = [(1, 2, 3)] *)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>introducing <a href="/articles/2022/08/28/ocaml-tips-multiple-let-bindings/">multiple <code class="language-plaintext highlighter-rouge">let</code> bindings</a> is a bit too verbose for my taste</li>
  <li>it took me a while to figure out <a href="https://stackoverflow.com/questions/1604270/what-is-the-difference-between-the-fun-and-function-keywords">the difference between <code class="language-plaintext highlighter-rouge">fun</code> and <code class="language-plaintext highlighter-rouge">function</code></a></li>
  <li>no syntax for list comprehensions (I often use those in Clojure, Haskell and Erlang, although I’m well aware they are not something essential)</li>
  <li>the syntax for array literals is somewhat weird (<code class="language-plaintext highlighter-rouge">[|1; 2; 3|]</code>) - I don’t like such usage of multiple symbols as literal boundaries, as this makes it harder for dev tools to figure out what you’re doing. The indexing syntax <code class="language-plaintext highlighter-rouge">arr.(i)</code> is slightly weird as well, as it doesn’t fit very well with the rest of the language.</li>
  <li>no first-class syntax for maps and sets (Clojure spoiled me on this front)</li>
</ul>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="c1">;; Clojure map</span><span class="w">
</span><span class="p">{</span><span class="no">:name</span><span class="w"> </span><span class="s">"Bruce Wayne"</span><span class="w"> </span><span class="no">:alias</span><span class="w"> </span><span class="s">"Batman"</span><span class="p">}</span><span class="w">

</span><span class="c1">;; Clojure set</span><span class="w">
</span><span class="o">#</span><span class="p">{</span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="p">}</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>nothing like Haskell’s typeclasses, which means in some cases you get different operators for similar types (e.g. numeric types):</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="c">(* int version *)</span>
<span class="k">let</span> <span class="n">average</span> <span class="n">a</span> <span class="n">b</span> <span class="o">=</span>
    <span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="o">.</span><span class="mi">0</span>

<span class="c">(* float version *)</span>
<span class="k">let</span> <span class="n">average</span> <span class="n">a</span> <span class="n">b</span> <span class="o">=</span>
    <span class="p">(</span><span class="n">a</span> <span class="o">+.</span> <span class="n">b</span><span class="p">)</span> <span class="o">/.</span> <span class="mi">2</span><span class="o">.</span><span class="mi">0</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>On the bright side - it’s really easy to spot float operations.</p>

<p>By the way, this also means you get different operators for things like string and
list concatenation, as you can’t use <code class="language-plaintext highlighter-rouge">+</code> which is defined in terms of integers:</p>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre><span class="s2">"Hello, "</span> <span class="o">^</span> <span class="s2">"world!"</span>

<span class="p">[</span><span class="mi">1</span><span class="p">;</span> <span class="mi">2</span><span class="p">]</span> <span class="o">@</span> <span class="p">[</span><span class="mi">3</span><span class="p">;</span> <span class="mi">4</span><span class="p">]</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h3 id="surprising-bits-of-syntax">Surprising Bits of Syntax</h3>

<p>Here are some things that I like overall, but I found somewhat surprising early on:</p>

<ul>
  <li>The <code class="language-plaintext highlighter-rouge">=</code> operator is used both for assignment and for structural comparisons:</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>

<span class="n">x</span> <span class="o">=</span> <span class="mi">5</span>
<span class="o">-</span> <span class="o">:</span> <span class="kt">bool</span> <span class="o">=</span> <span class="bp">true</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>
    <p>The operators <code class="language-plaintext highlighter-rouge">==</code> and <code class="language-plaintext highlighter-rouge">!=</code> are used for identity comparison (whether you’re comparing something with itself), when in some other languages they are used for structural comparison. Not exactly wild, but not very common either.</p>
  </li>
  <li>
    <p>The operator for structural inequality is <code class="language-plaintext highlighter-rouge">&lt;&gt;</code>. Although it’s not like Haskell’s <code class="language-plaintext highlighter-rouge">\=</code> is particularly common either.</p>
  </li>
</ul>

<p>By the way, when it comes to learning OCaml’s operators I can’t recommend <a href="https://www.craigfe.io/operator-lookup/">this site</a> highly enough!</p>

<ul>
  <li>Several modules in the standard library have two versions - one where the functions are using positional arguments and one where the functions are taking labeled (named) arguments (e.g. <code class="language-plaintext highlighter-rouge">String</code> and <code class="language-plaintext highlighter-rouge">StringLabels</code>). Not exactly a syntax matter, but definitely a peculiarity.</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="o">#</span> <span class="nn">List</span><span class="p">.</span><span class="n">map</span><span class="p">;;</span>
<span class="o">-</span> <span class="o">:</span> <span class="p">(</span><span class="k">'</span><span class="n">a</span> <span class="o">-&gt;</span> <span class="k">'</span><span class="n">b</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="k">'</span><span class="n">a</span> <span class="kt">list</span> <span class="o">-&gt;</span> <span class="k">'</span><span class="n">b</span> <span class="kt">list</span> <span class="o">=</span> <span class="o">&lt;</span><span class="k">fun</span><span class="o">&gt;</span>
<span class="o">#</span> <span class="nn">ListLabels</span><span class="p">.</span><span class="n">map</span><span class="p">;;</span>
<span class="o">-</span> <span class="o">:</span> <span class="n">f</span><span class="o">:</span><span class="p">(</span><span class="k">'</span><span class="n">a</span> <span class="o">-&gt;</span> <span class="k">'</span><span class="n">b</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="k">'</span><span class="n">a</span> <span class="kt">list</span> <span class="o">-&gt;</span> <span class="k">'</span><span class="n">b</span> <span class="kt">list</span> <span class="o">=</span> <span class="o">&lt;</span><span class="k">fun</span><span class="o">&gt;</span>

<span class="o">#</span> <span class="nn">String</span><span class="p">.</span><span class="n">sub</span><span class="p">;;</span>
<span class="o">-</span> <span class="o">:</span> <span class="kt">string</span> <span class="o">-&gt;</span> <span class="kt">int</span> <span class="o">-&gt;</span> <span class="kt">int</span> <span class="o">-&gt;</span> <span class="kt">string</span> <span class="o">=</span> <span class="o">&lt;</span><span class="k">fun</span><span class="o">&gt;</span>
<span class="o">#</span> <span class="nn">StringLabels</span><span class="p">.</span><span class="n">sub</span><span class="p">;;</span>
<span class="o">-</span> <span class="o">:</span> <span class="kt">string</span> <span class="o">-&gt;</span> <span class="n">pos</span><span class="o">:</span><span class="kt">int</span> <span class="o">-&gt;</span> <span class="n">len</span><span class="o">:</span><span class="kt">int</span> <span class="o">-&gt;</span> <span class="kt">string</span> <span class="o">=</span> <span class="o">&lt;</span><span class="k">fun</span><span class="o">&gt;</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li>Having two ways to create anonymous functions - via <code class="language-plaintext highlighter-rouge">fun</code> and <code class="language-plaintext highlighter-rouge">function</code>.
Basically <code class="language-plaintext highlighter-rouge">function</code> only allows for one argument, but allows for pattern matching, while <code class="language-plaintext highlighter-rouge">fun</code> is the more general way to define a function.</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="n">f</span> <span class="o">=</span> <span class="k">function</span> <span class="n">x</span> <span class="o">-&gt;</span> <span class="n">x</span> <span class="o">*</span> <span class="mi">5</span>

<span class="k">let</span> <span class="k">rec</span> <span class="n">last</span> <span class="o">=</span> <span class="k">function</span>
  <span class="o">|</span> <span class="bp">[]</span> <span class="o">-&gt;</span> <span class="nc">None</span>
  <span class="o">|</span> <span class="p">[</span> <span class="n">x</span> <span class="p">]</span> <span class="o">-&gt;</span> <span class="nc">Some</span> <span class="n">x</span>
  <span class="o">|</span> <span class="n">_</span> <span class="o">::</span> <span class="n">t</span> <span class="o">-&gt;</span> <span class="n">last</span> <span class="n">t</span>

<span class="k">let</span> <span class="n">f</span> <span class="o">=</span> <span class="k">fun</span> <span class="n">x</span> <span class="o">-&gt;</span> <span class="n">x</span> <span class="o">*</span> <span class="mi">5</span>
<span class="k">let</span> <span class="n">g</span> <span class="o">=</span> <span class="k">fun</span> <span class="n">x</span> <span class="n">y</span> <span class="o">-&gt;</span> <span class="n">x</span> <span class="o">*</span> <span class="n">y</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>You can learn more about the differences between <code class="language-plaintext highlighter-rouge">fun</code> and <code class="language-plaintext highlighter-rouge">function</code> <a href="https://stackoverflow.com/questions/1604270/what-is-the-difference-between-the-fun-and-function-keywords">here</a>.</p>

<h3 id="reason">Reason</h3>

<blockquote>
  <p>Reason is a programming language powered by OCaml’s strong type system, and has a syntax designed to feel familiar to people coming from JavaScript or C-family languages.</p>

  <p>– https://reasonml.github.io</p>
</blockquote>

<p>In other words Reason allows you to leverage the OCaml ecosystem with a syntax that might be more familiar to some people. Think something like the relationship between Elixir and Erlang. Here’s some code written in Reason:</p>

<pre><code class="language-reason">let launchMissile = () =&gt; {
  someSideEffects();
  print_endline("Missiles have been launched!");
};

launchMissile();

type schoolPerson = Teacher | Director | Student(string);

let greeting = person =&gt;
  switch (person) {
  | Teacher =&gt; "Hey Professor!"
  | Director =&gt; "Hello Director."
  | Student("Richard") =&gt; "Still here Ricky?"
  | Student(anyOtherName) =&gt; "Hey, " ++ anyOtherName ++ "."
  };
</code></pre>

<p>Whether this is any better than OCaml’s syntax is up to you to decide. Personally,
I’m not a fan of Reason, but if it helps grow the OCaml community then it’s a great
idea in my book.</p>

<h3 id="syntax-summary">Syntax Summary</h3>

<blockquote>
  <p>This too shall pass.</p>

  <p>– Ancient Persian adage</p>
</blockquote>

<p>Overall, there’s not much to write home about when it comes to OCaml’s syntax - it certainly won’t shock anyone the way Lisp or Prolog would. OCaml has a very long legacy as part of the ML-family of languages and I guess it accumulated a few syntactic oddities over time. I don’t expect any changes to happen and I got used to whatever I considered to be “weird” fairly quickly.</p>

<h2 id="ocaml-platform-compiler-libraries-frameworks">OCaml Platform (Compiler, Libraries, Frameworks)</h2>

<p>In brief:</p>

<ul>
  <li>the compiler is super fast, but that was expected given that OCaml is famous for it.</li>
  <li>the compiler is also quite helpful, although its messages can definitely be more descriptive in some cases.</li>
  <li>the standard library is quite basic and that’s probably my main gripe with the language so far - e.g. the support for things like string manipulation (the standard library doesn’t support Unicode strings) and regular expressions is abysmal given what you’d get with most other languages today.</li>
  <li>the above has spurred the creation of all sorts of extensions and replacements to the standard library, which creates a lot of learning overhead and fragmentation. I was amused to see that the popular book <a href="https://dev.realworldocaml.org/">Real World OCaml</a> directly recommends replacing the standard library with a third-party alternative.</li>
  <li>the package manager <a href="https://opam.ocaml.org">Opam</a> is relatively easy to use, although using switches (isolated OCaml environments) takes a while to get used to. I’m still not sure that should be part of the responsibilities of a package manager, but it gets the job done. Opam could use some better <a href="https://opam.ocaml.org/doc/Manual.html">documentation</a>.</li>
</ul>

<p>There’s also support to target <a href="https://github.com/ocsigen/js_of_ocaml">JavaScript from OCaml sources</a>, which looks interesting, but I haven’t tried it yet. <a href="https://rescript-lang.org/">ReScript</a> (basically Reason that targets JavaScript) is also appealing in this space, even if it’s only tangentially connected to OCaml.<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup></p>

<p>One common complaint about OCaml is its lack of support for parallel programming - OCaml currently doesn’t natively support multiple OS-level OCaml threads running simultaneously. A global lock prevents multiple OCaml threads from running at once. The good news is that <a href="https://discuss.ocaml.org/t/ocaml-5-0-zeroth-alpha-release/10026">OCaml 5.0</a> is right around the corner and its flagship feature is native support for multicore parallelism! This <a href="https://github.com/ocaml-multicore/parallel-programming-in-multicore-ocaml">article</a> is a fantastic overview of Multicore OCaml. Exciting times ahead!</p>

<p>Back to the present - today OCaml has pretty decent support for <a href="https://ocamlverse.github.io/content/parallelism.html#concurrency">concurrent programming</a> (most notably via the library <a href="https://github.com/ocsigen/lwt">Lwt</a>). Moreover, my experience with Ruby has taught me that one can get pretty far and build a lot of useful software despite the presence of a global lock. And don’t even get me started on the performance difference between Ruby and OCaml…</p>

<p>There are plenty of third-party OCaml libraries, although most of them seem somewhat under-maintained and under-documented. I’ve noticed that for many libraries all the documentation you’ll find is just API signatures - e.g. <a href="https://ocaml.org/p/re/1.10.4/doc/Re/Perl/index.html">this</a>.</p>

<p>The only framework I looked into was <a href="https://aantron.github.io/dream/">Dream</a> (a web framework). It looked pretty nice, but it’s still in alpha, has only one developer and it seems he has not been very active lately. Oh, well - that’s a common problem in smaller programming communities.</p>

<h3 id="a-note-about-standard-libraries">A Note About Standard Libraries</h3>

<p>OCaml’s standard library has few fans and lots of competition.</p>

<p>It seems that today the most popular complete alternative of OCaml’s standard library is <a href="https://opensource.janestreet.com/base/">Base</a> by Jane Street. There’s also <a href="https://opensource.janestreet.com/core/">Core</a> which is a superset of <code class="language-plaintext highlighter-rouge">Base</code> and features even more functionality. As I’ve started out by reading the RWO book initially I was
using <code class="language-plaintext highlighter-rouge">Base</code> all the time, but I’ve stopped using it since. Not that the library is bad or anything - I just wanted to give myself some time to assess for myself the problems that <code class="language-plaintext highlighter-rouge">Base</code> supposedly solves, plus consider other alternatives.</p>

<p>I’ve recently discovered <a href="https://github.com/c-cube/ocaml-containers">Containers</a>, which extends OCaml’s standard library instead of replacing it completely. I plan to make heavier use of it going forward.</p>

<h2 id="development-tooling">Development Tooling</h2>

<p>I’d say the development tooling for OCaml is pretty decent, although I wouldn’t go as far as saying it’s great. I’ve documented my current Emacs setup <a href="/articles/2022/08/23/setting-up-emacs-for-ocaml-development/">here</a> and I’ve also played a bit with the “official” VS Code extension to get a feel for OCaml-LSP.</p>

<p>Without a doubt, <a href="https://ocaml.github.io/merlin/">Merlin</a> is the primary workhorse of the development tools and it’s definitely pretty awesome. It provides editor features like code completion, typing information, navigation to definition, refactoring, code generation, linting, etc. It’s a beast! Emacs’s <code class="language-plaintext highlighter-rouge">merlin-mode</code> reminds me in a way of CIDER (for Clojure).</p>

<p><a href="https://github.com/ocaml-community/utop/">utop</a> is a good toplevel (REPL) and has become a major part of my OCaml toolbox. It’d be nice if down the road someone improves a bit the default <code class="language-plaintext highlighter-rouge">ocaml</code> toplevel, which is extremely basic by every modern standard.</p>

<p><a href="https://dune.build/">Dune</a> is a solid build tool and I definitely enjoyed working with it. It does need better documentation, though, as I learned more about it (and Opam) from third-party documentation instead of from their official documentation.</p>

<p>For testing I can recommend the following libraries:</p>

<ul>
  <li><a href="https://github.com/janestreet/ppx_inline_test">ppx_inline_test</a> for simple tests that you can place alongside your code, while you’re developing it:</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="k">let</span> <span class="n">is_prime</span> <span class="o">=</span> <span class="o">&lt;</span><span class="n">magic</span><span class="o">&gt;</span>

<span class="k">let</span><span class="o">%</span><span class="n">test</span> <span class="n">_</span> <span class="o">=</span> <span class="n">is_prime</span> <span class="mi">5</span>
<span class="k">let</span><span class="o">%</span><span class="n">test</span> <span class="n">_</span> <span class="o">=</span> <span class="n">is_prime</span> <span class="mi">7</span>
<span class="k">let</span><span class="o">%</span><span class="n">test</span> <span class="n">_</span> <span class="o">=</span> <span class="n">not</span> <span class="p">(</span><span class="n">is_prime</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">let</span><span class="o">%</span><span class="n">test</span> <span class="n">_</span> <span class="o">=</span> <span class="n">not</span> <span class="p">(</span><span class="n">is_prime</span> <span class="mi">8</span><span class="p">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li><a href="https://github.com/janestreet/ppx_expect">ppx_expect</a> is a framework for writing tests in OCaml, similar to Cram. Expect-tests mimic the existing inline tests framework with the <code class="language-plaintext highlighter-rouge">let%expect_test</code> construct. The body of an expect-test can contain output-generating code, interleaved with <code class="language-plaintext highlighter-rouge">%expect</code> extension expressions to denote the expected output:</li>
</ul>

<div class="language-ocaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">open</span> <span class="nc">Core</span>

<span class="k">let</span><span class="o">%</span><span class="n">expect_test</span> <span class="s2">"addition"</span> <span class="o">=</span>
  <span class="n">printf</span> <span class="s2">"%d"</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span><span class="p">);</span>
  <span class="p">[</span><span class="o">%</span><span class="n">expect</span> <span class="p">{</span><span class="o">|</span> <span class="mi">4</span> <span class="o">|</span><span class="p">}]</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<ul>
  <li><a href="https://github.com/mirage/alcotest">alcotest</a> is my favorite testing framework so far. It’s really easy to work with and it has beautiful output.</li>
</ul>

<p><img src="https://raw.githubusercontent.com/mirage/alcotest/main/.meta/error.png" alt="alcotest.png" /></p>

<p>I think for most people a combination of VS Code + a <code class="language-plaintext highlighter-rouge">utop</code> and <code class="language-plaintext highlighter-rouge">dune</code> (e.g. <code class="language-plaintext highlighter-rouge">dune runtest -w</code>) running in dedicated terminals will provide the optimal development experience. This way you’ll be able to play with some small snippets of OCaml code in your toplevel and get instant feedback from your test suite on every change you make. Of course, if you can stomach Emacs - even better. ;-)</p>

<h2 id="community">Community</h2>

<p>The OCaml community is tiny by the standards of more popular programming languages, but I really enjoyed all my interactions with its members. I’ve been mostly using the official <a href="https://discord.gg/cCYQbqN">Discord</a> and <a href="https://discus.ocaml.org">Discourse</a>, but there are plenty of <a href="https://ocaml.org/community">other options</a> for you to choose from.</p>

<p>I’ve gotten plenty of answers to every question I asked and I really enjoyed learning more about <a href="https://discuss.ocaml.org/t/whats-your-development-workflow/10358">the workflows OCaml developers have</a>, as finding a productive workflow was one of the things I’ve struggled with early on.<sup id="fnref:2"><a href="#fn:2" class="footnote" rel="footnote" role="doc-noteref">2</a></sup></p>

<p>I also learned a lot simply by hanging out there and perusing the topics that were being posted and channels like <code class="language-plaintext highlighter-rouge">#general</code>, <code class="language-plaintext highlighter-rouge">#beginners</code> and <code class="language-plaintext highlighter-rouge">#share</code>.</p>

<p>One final community resource I’d like to recommend is the awesome wiki <a href="https://ocamlverse.github.io/">OCamlverse</a>. It complements really well the information on the official OCaml site.</p>

<h2 id="learning-resources">Learning Resources</h2>

<p>You certainly won’t find dozens of books, hundreds of tutorials and a few online courses for OCaml. It seems that the most popular resources to learn OCaml are:</p>

<ul>
  <li><a href="https://dev.realworldocaml.org/">Real World OCaml</a>: A free book about OCaml, that’s considered one of the best starting points in the community. On the flip side - it’s somewhat controversial for its decision to advocate the use of an alternative standard library (<code class="language-plaintext highlighter-rouge">Base</code> from Jane Street).</li>
  <li><a href="https://cs3110.github.io/textbook/cover.html">Cornell University’s course on OCaml</a>: It’s taught at the university, but the textbook and the video lectures are freely available online. I enjoyed the course, although it’s clearly geared towards students and not practicing programmers.</li>
  <li><a href="https://ocamlverse.github.io/">OCamlverse</a>: An OCaml community wiki. The articles there give you a lot of practical pointers about day-to-day OCaml programming and complement nicely the tutorial/manual texts.</li>
</ul>

<p>There’s also some <a href="https://ocaml.org/docs/up-and-running">tutorial-like section</a> on the official site, but it could have been structured better in my opinion.</p>

<p>I definitely think there’s a lot of room for more resources for newcomers and
that’s one area that will need work if OCaml is grow its mindshare. When everyone in the community is discussing the merits of a single book (RWO), you immediately figure out the problem is not the contents of the book, but rather the lack of alternative books.</p>

<p>While Clojure and Haskell are considered niche languages as well, there are a lot more learning resources of every flavor for them. I think that even Erlang has more resources than OCaml these days.</p>

<h2 id="a-closing-note-about-libraries">A Closing Note About Libraries</h2>

<p>A while ago I made a comment about the standard library situation <a href="https://discuss.ocaml.org/t/what-i-dislike-about-ocaml/10248/50?u=bbatsov">on Discourse</a> that triggered a follow-up conversation about two things:</p>

<ul>
  <li>that the standard library wasn’t a major focus for OCaml’s team and they prefer for libraries to be developed independently by experts in a particular area (fair enough)</li>
  <li>that it’s actually easy to find the right libraries for each problem (it was mostly focused on strings)</li>
</ul>

<p>While, I can agree with the first point in principle, I’d still expect at least a set of well-known libraries to be widely recommended, etc. Digging for libraries while learning a language is not always fun and sometimes leads in the wrong direction. On the second point - I was amused to see back-to-back comments that were contradicting one another. First I got the follow suggestion:</p>

<blockquote>
  <p>For example, for Unicode support you need to decide between Camomile and <code class="language-plaintext highlighter-rouge">uutf</code>, depending on your needs.</p>
</blockquote>

<p>And then it got this response:</p>

<blockquote>
  <p>I’m not sure I understand this comment. I don’t think there’s anything to decide between the two things you mention.</p>

  <p>First the only thing that uutf provides, namely UTF codecs is nowaday available in <code class="language-plaintext highlighter-rouge">Stdlib.Buffer</code> and <code class="language-plaintext highlighter-rouge">Stdlib.String</code>. Except if you rely on the non-blocking stuff, which most people don’t, there’s no reason to use uutf anymore. People should move away from it and consider it deprecated.</p>

  <p>Second if you need to access Unicode character data or perform normalisations, AFAIK Camomile has not updated its Unicode data for quite some time. Last time I looked it was Unicode 3.2 which was released in 2002, twenty years ago.</p>

  <p>Since then thousands of characters, dozen of scripts and few new characters properties have been added. This makes Camomile entirely impractical to deal with the Unicode of today.</p>

  <p>So I don’t think there’s any choice to be had here. You will be better served by <code class="language-plaintext highlighter-rouge">uucp</code>, <code class="language-plaintext highlighter-rouge">uunf</code> and <code class="language-plaintext highlighter-rouge">uuseg</code> (this one not provided by Camomile).<sup id="fnref:3"><a href="#fn:3" class="footnote" rel="footnote" role="doc-noteref">3</a></sup></p>
</blockquote>

<p>And finally the acknowledgment:</p>

<blockquote>
  <p>Exactly the kind of thing newcomers or casual users would absolutely never know unless they did a lot of research.</p>
</blockquote>

<p>Indeed! Clearly finding the right libraries is not trivial in this case. Hopefully the situation will be improved with time.</p>

<h2 id="conclusion">Conclusion</h2>

<p>I can’t say that my initial experience with OCaml was mind-blowing, but it was definitely pleasant and with time the language grows on me. I miss the simplicity and uniformity of Clojure (still my favorite programming language by far) or some of Haskell’s goodies (e.g. typeclasses, <a href="https://hackage.haskell.org/">Hackage</a> and <code class="language-plaintext highlighter-rouge">ghci</code>), but I feel OCaml strikes a very good balance between functional programming, pragmatism and performance.</p>

<p>At this point I’ve seen enough to be convinced to continue with my exploration of OCaml and dig deeper. Keep hacking!</p>

<hr />

<p>You can discuss the article on <a href="https://news.ycombinator.com/item?id=32636682">Hacker News</a> or <a href="https://discuss.ocaml.org/t/ocaml-at-first-glance/10396">OCaml’s Discourse</a>.</p>
<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p><a href="https://ersin-akinci.medium.com/confused-about-rescript-rescript-reason-reasonml-and-bucklescript-explained-ab4230555230">This article</a> covers well the confusing topic of how Reason, ReasonML and ReScript relate to one another. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
    <li id="fn:2">
      <p>Pro tip - <code class="language-plaintext highlighter-rouge">dune runtest -w --no-buffer</code> is essential! <a href="#fnref:2" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
    <li id="fn:3">
      <p>Don’t even get me started on names like <code class="language-plaintext highlighter-rouge">uucp</code>, <code class="language-plaintext highlighter-rouge">uund</code> and <code class="language-plaintext highlighter-rouge">uuseg</code>. :-) Naming continues to be hard! <a href="#fnref:3" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Bozhidar Batsov</name></author><category term="OCaml" /><category term="Clojure" /><category term="Haskell" /><summary type="html"><![CDATA[You never get a second chance to make a first impression.]]></summary></entry><entry><title type="html">Clojure Tricks: Number to Digits</title><link href="https://batsov.com/articles/2022/08/01/clojure-tricks-number-to-digits/" rel="alternate" type="text/html" title="Clojure Tricks: Number to Digits" /><published>2022-08-01T10:42:00+03:00</published><updated>2022-08-01T10:42:00+03:00</updated><id>https://batsov.com/articles/2022/08/01/clojure-tricks-number-to-digits</id><content type="html" xml:base="https://batsov.com/articles/2022/08/01/clojure-tricks-number-to-digits/"><![CDATA[<p>If you’re into programming puzzles you probably know that there’s a whole class
of problems about doing something (e.g. some calculations) with the digits of a
number. This means you need to break down a number into its digits first. I’ve
always assumed that those problems exist just because decomposing a number to
its digits is a classic example of recursion:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">digits</span><span class="w"> </span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="w">
  </span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">&lt;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w">
    </span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="w">
    </span><span class="p">(</span><span class="nb">conj</span><span class="w"> </span><span class="p">(</span><span class="nf">digits</span><span class="w"> </span><span class="p">(</span><span class="nb">quot</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="mi">10</span><span class="p">))</span><span class="w"> </span><span class="p">(</span><span class="nb">rem</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="mi">10</span><span class="p">))))</span><span class="w">

</span><span class="p">(</span><span class="nf">digits</span><span class="w"> </span><span class="mi">3361346435</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; [3 3 6 1 3 4 6 4 3 5]</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>That being said, I’ve also noticed that many people are approaching the problem
differently when faced with it (including me) - they typically convert the
number to string and then convert back the digit characters into
numbers. Probably the simplest way to do this is something like this:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">digits</span><span class="w"> </span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="w">
  </span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="o">#</span><span class="p">(</span><span class="nf">read-string</span><span class="w"> </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="n">%</span><span class="p">))</span><span class="w"> </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="n">n</span><span class="p">)))</span><span class="w">

</span><span class="p">(</span><span class="nf">digits</span><span class="w"> </span><span class="mi">3361346435</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; (3 3 6 1 3 4 6 4 3 5)</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Notably, this solution doesn’t require using the Java interop or any clever tricks.
If you know Java’s API a bit better you might think of leveraging <code class="language-plaintext highlighter-rouge">Character/digit</code> instead:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">digits</span><span class="w"> </span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="w">
  </span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="o">#</span><span class="p">(</span><span class="nf">Character/digit</span><span class="w"> </span><span class="n">%</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="n">n</span><span class="p">)))</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Much simpler, right? Now it’s time for the final approach to solving the problem that is relatively common - namely a simple but clever trick to convert digit characters into numbers:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">char-&gt;int</span><span class="w"> </span><span class="p">[</span><span class="n">c</span><span class="p">]</span><span class="w">
  </span><span class="p">(</span><span class="nb">-</span><span class="w"> </span><span class="p">(</span><span class="nb">int</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="mi">48</span><span class="p">))</span><span class="w">

</span><span class="p">(</span><span class="k">defn</span><span class="w"> </span><span class="n">digits</span><span class="w"> </span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="w">
  </span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="n">char-&gt;int</span><span class="w"> </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="n">n</span><span class="p">)))</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>This relies on the fact that the integer value for <code class="language-plaintext highlighter-rouge">\0</code> is 48, for <code class="language-plaintext highlighter-rouge">\1</code> is 49 and so on. For some reason that’s my favorite solution - probably because I love programming puzzles and I like (occasionally) writing unreadable code.</p>

<p>So, who has a different approach for converting a number to its digits? I’d love to hear about it!</p>]]></content><author><name>Bozhidar Batsov</name></author><category term="Clojure" /><category term="Trick" /><summary type="html"><![CDATA[If you’re into programming puzzles you probably know that there’s a whole class of problems about doing something (e.g. some calculations) with the digits of a number. This means you need to break down a number into its digits first. I’ve always assumed that those problems exist just because decomposing a number to its digits is a classic example of recursion:]]></summary></entry><entry><title type="html">Clojure Tricks: Zipping Things Together</title><link href="https://batsov.com/articles/2022/07/31/clojure-tricks-zipping-things-together/" rel="alternate" type="text/html" title="Clojure Tricks: Zipping Things Together" /><published>2022-07-31T14:10:00+03:00</published><updated>2022-07-31T14:10:00+03:00</updated><id>https://batsov.com/articles/2022/07/31/clojure-tricks-zipping-things-together</id><content type="html" xml:base="https://batsov.com/articles/2022/07/31/clojure-tricks-zipping-things-together/"><![CDATA[<p>Many programming languages have a function for combining the elements of multiple collections (e.g. arrays or lists) together. Typically this function is named <code class="language-plaintext highlighter-rouge">zip</code>. Here’s an example from Haskell:</p>

<div class="language-haskell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">zip</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="p">[</span><span class="sc">'a'</span><span class="p">,</span> <span class="sc">'b'</span><span class="p">]</span> <span class="c1">-- =&gt; [(1, 'a'), (2, 'b')]</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Clojure doesn’t have a <code class="language-plaintext highlighter-rouge">zip</code> function in the standard library, which leads many newcomers to the language to wonder what to use in its place. The answer is quite simple:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="rouge-code"><pre><span class="c1">;; let's zip a couple of lists, even if no one really uses them in Clojure</span><span class="w">
</span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="nb">vector</span><span class="w"> </span><span class="o">'</span><span class="p">(</span><span class="nf">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="o">'</span><span class="p">(</span><span class="nf">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="p">))</span><span class="w">
</span><span class="c1">;; =&gt; ([1 4] [2 5] [3 6])</span><span class="w">

</span><span class="c1">;; works with vectors as well</span><span class="w">
</span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="nb">vector</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="p">])</span><span class="w">
</span><span class="c1">;; =&gt; ([1 4] [2 5] [3 6])</span><span class="w">

</span><span class="c1">;; actually this works with anything seq-able</span><span class="w">
</span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="nb">vector</span><span class="w"> </span><span class="s">"this"</span><span class="w"> </span><span class="s">"that"</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; ([\t \t] [\h \h] [\i \a] [\s \t])</span><span class="w">

</span><span class="c1">;; and you can mix and match different seq types</span><span class="w">
</span><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="nb">vector</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="p">]</span><span class="w"> </span><span class="s">"this"</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; ([1 \t] [2 \h] [3 \i] [4 \s])</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>The function <code class="language-plaintext highlighter-rouge">vector</code> takes a variable number of arguments and produces a vector out of them. And vectors happen to be the idiomatic way to represent the concept of tuples (common in other functional programming languages) in Clojure. By the way, you can zip as many sequences together as your heart desires:<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup></p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="nb">vector</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">7</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="mi">9</span><span class="p">])</span><span class="w">
</span><span class="c1">;; =&gt; ([1 4 7] [2 5 8] [3 6 9])</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>And they don’t all have to be of the same length either:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nb">map</span><span class="w"> </span><span class="nb">vector</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">7</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="mi">9</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">10</span><span class="w"> </span><span class="mi">11</span><span class="p">])</span><span class="w">
</span><span class="c1">;; =&gt; ([1 4 7 10] [2 5 8 11])</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>One more thing… Clojure also has a function named <code class="language-plaintext highlighter-rouge">zipmap</code> that can zip a couple of seqs into a map:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nb">zipmap</span><span class="w"> </span><span class="p">[</span><span class="no">:a</span><span class="w"> </span><span class="no">:b</span><span class="w"> </span><span class="no">:c</span><span class="w"> </span><span class="no">:d</span><span class="w"> </span><span class="no">:e</span><span class="p">]</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="p">])</span><span class="w">
</span><span class="c1">;; =&gt; {:a 1, :b 2, :c 3, :d 4, :e 5}</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>That’s all I have for you today. Zip long and prosper!</p>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>That’s a big advantage over Haskell’s <code class="language-plaintext highlighter-rouge">zip</code> mentioned earlier, as it can only combine two lists. There’s a similar function called <code class="language-plaintext highlighter-rouge">zip3</code> that can combine three lists. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Bozhidar Batsov</name></author><category term="Clojure" /><category term="Trick" /><summary type="html"><![CDATA[Many programming languages have a function for combining the elements of multiple collections (e.g. arrays or lists) together. Typically this function is named zip. Here’s an example from Haskell:]]></summary></entry><entry><title type="html">Clojure Tricks: Replace in String</title><link href="https://batsov.com/articles/2022/07/31/clojure-tricks-replace-in-string/" rel="alternate" type="text/html" title="Clojure Tricks: Replace in String" /><published>2022-07-31T11:12:00+03:00</published><updated>2022-07-31T11:12:00+03:00</updated><id>https://batsov.com/articles/2022/07/31/clojure-tricks-replace-in-string</id><content type="html" xml:base="https://batsov.com/articles/2022/07/31/clojure-tricks-replace-in-string/"><![CDATA[<p>Today I saw a clever bit of Clojure code involving <code class="language-plaintext highlighter-rouge">clojure.string/replace</code>, that reminded me how powerful the Clojure standard library is. I guess pretty much everyone knows that <code class="language-plaintext highlighter-rouge">replace</code> is normally used to replace some part of a string using a regular expression to describe what exactly to replace:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nf">str/replace</span><span class="w"> </span><span class="s">"OCaml rocks!"</span><span class="w"> </span><span class="o">#</span><span class="s">"([Hh]askell)|([Oo][Cc]aml)"</span><span class="w"> </span><span class="s">"Clojure"</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; "Clojure rocks!"</span><span class="w">

</span><span class="c1">;; we can refer to the match groups in the replacement string</span><span class="w">
</span><span class="p">(</span><span class="nf">str/replace</span><span class="w"> </span><span class="s">"Haskell rocks!"</span><span class="w"> </span><span class="o">#</span><span class="s">"([Hh]askell)|([Oo][Cc]aml)"</span><span class="w"> </span><span class="s">"$1 is nice, but Clojure"</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; "Haskell is nice, but Clojure rocks!"</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Pretty useful and pretty straightforward. But wait, there’s more! I had forgotten you can actually use a function for the replacement, which allows us to do more sophisticated things. Here’s how we can capitalize every word with 5 or more letters in a string:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nf">str/replace</span><span class="w"> </span><span class="s">"master of Clojure is pulling the strings"</span><span class="w"> </span><span class="o">#</span><span class="s">"\w{5,}"</span><span class="w"> </span><span class="n">str/upper-case</span><span class="p">)</span><span class="w">
</span><span class="c1">;; =&gt; "MASTER of CLOJURE is PULLING the STRINGS"</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>If you’ve got more match groups in your regular expression you can use all of them in the function that you’re using to generate the replacements:</p>

<div class="language-clojure highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
</pre></td><td class="rouge-code"><pre><span class="p">(</span><span class="nf">str/replace</span><span class="w"> </span><span class="s">"pom kon sor"</span><span class="w"> </span><span class="o">#</span><span class="s">"(.)o(.)"</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">[[</span><span class="n">_</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">b</span><span class="p">]]</span><span class="w"> </span><span class="p">(</span><span class="nb">str</span><span class="w"> </span><span class="p">(</span><span class="nf">str/upper-case</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="s">"-o-"</span><span class="w"> </span><span class="p">(</span><span class="nf">str/upper-case</span><span class="w"> </span><span class="n">b</span><span class="p">))))</span><span class="w">
</span><span class="s">"P-o-M K-o-N S-o-R"</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>

<p>Note here the use of destructuring to account that each match is essentially a vector of the full match and each group match (e.g. <code class="language-plaintext highlighter-rouge">["pom" "p" "m"]</code>).</p>

<p>That’s all I have for you today! Feel free to share with me more fun usages of <code class="language-plaintext highlighter-rouge">replace</code>!</p>]]></content><author><name>Bozhidar Batsov</name></author><category term="Clojure" /><category term="Trick" /><summary type="html"><![CDATA[Today I saw a clever bit of Clojure code involving clojure.string/replace, that reminded me how powerful the Clojure standard library is. I guess pretty much everyone knows that replace is normally used to replace some part of a string using a regular expression to describe what exactly to replace:]]></summary></entry><entry><title type="html">Interactive Programming in a Nutshell</title><link href="https://batsov.com/articles/2021/12/24/interactive-programming-in-a-nutshell/" rel="alternate" type="text/html" title="Interactive Programming in a Nutshell" /><published>2021-12-24T10:16:00+02:00</published><updated>2021-12-24T10:16:00+02:00</updated><id>https://batsov.com/articles/2021/12/24/interactive-programming-in-a-nutshell</id><content type="html" xml:base="https://batsov.com/articles/2021/12/24/interactive-programming-in-a-nutshell/"><![CDATA[<p>Today I came across this great summary of the concept of interactive programming:</p>

<blockquote>
  <p>Development in Common Lisp is interactive. There’s no separate compile/run/debug cycle. Instead of that, the program is developed while it runs. Compilation is incremental, and functions can be created and updated on the fly. As the program is running, all objects are available and can be inspected all the time. This is much more than a simple REPL; the whole environment, from the IDE to the language is prepared for this type of development.</p>

  <p>– https://common-lisp.net/features</p>
</blockquote>

<p>Obviously, this is not something specific to Common Lisp and it applies to most Lisp dialects and many other programming languages.</p>

<p>I was first exposed to interactive programming when studying Common Lisp in 2005. Back then I came across <a href="https://www.youtube.com/watch?v=NUpAvqa5hQw">a SLIME screencast</a> by Marco Baringer that blew my mind (I was mostly programming in C++ and Java at the time).<sup id="fnref:1"><a href="#fn:1" class="footnote" rel="footnote" role="doc-noteref">1</a></sup> Today such a workflow feels just as magical and special as it did all those years ago.</p>

<p>For me interactive programming is probably <em>the most important advantage</em> of Lisps and Emacs over other programming languages and editors. I cannot imagine any productive workflow without it!</p>

<p>I’ve written a bit about interactive programming with Clojure and Emacs <a href="https://docs.cider.mx/cider/usage/interactive_programming.html">here</a>. Still, I always feel that this is one of those concepts that you can’t really understand until you’ve experienced it. Just like the Matrix.</p>

<div class="footnotes" role="doc-endnotes">
  <ol>
    <li id="fn:1">
      <p>I still have it on my todo list to record a similar screencast for CIDER. <a href="#fnref:1" class="reversefootnote" role="doc-backlink">&#8617;&#xfe0e;</a></p>
    </li>
  </ol>
</div>]]></content><author><name>Bozhidar Batsov</name></author><category term="Lisp" /><category term="Clojure" /><category term="Emacs" /><category term="Interactive Programming" /><summary type="html"><![CDATA[Today I came across this great summary of the concept of interactive programming:]]></summary></entry><entry><title type="html">Advent of Code 2021</title><link href="https://batsov.com/articles/2021/12/02/advent-of-code-2021/" rel="alternate" type="text/html" title="Advent of Code 2021" /><published>2021-12-02T11:29:00+02:00</published><updated>2021-12-02T11:29:00+02:00</updated><id>https://batsov.com/articles/2021/12/02/advent-of-code-2021</id><content type="html" xml:base="https://batsov.com/articles/2021/12/02/advent-of-code-2021/"><![CDATA[<p>I love programming puzzles, even if I’ve never been particularly good at solving
them. For me they were always a good way to gain some practice with a new
programming language and to push myself to think for things unrelated to my job
or my own projects. Optimizing the performance of solutions to puzzles and
reading the solutions of other people is a very educational experience as
well. In recent years I rarely had time to play with puzzles, but this year I’ll
try to find a bit of time for <a href="https://adventofcode.com/2021">Advent of Code</a>.</p>

<p>I’ll be using Clojure to solve the puzzles and I’ll post my solutions to <a href="https://github.com/bbatsov/advent-of-code2021">GitHub</a>. You can say that I’m also looking for an excuse to use <a href="https://github.com/clojure-emacs/cider/">CIDER</a> for anything else besides developing CIDER.</p>

<p>I already solved the first 4 puzzles (you get to solve 2 each day) and this was a lot of fun. Let’s see how far I’ll manage to get. If I manage to solve half the puzzles I’ll consider this a great success!</p>

<p>P.S. Don’t worry, I don’t plan to spam you daily with puzzle solutions!</p>]]></content><author><name>Bozhidar Batsov</name></author><category term="Meta" /><category term="Clojure" /><category term="Advent of Code" /><summary type="html"><![CDATA[I love programming puzzles, even if I’ve never been particularly good at solving them. For me they were always a good way to gain some practice with a new programming language and to push myself to think for things unrelated to my job or my own projects. Optimizing the performance of solutions to puzzles and reading the solutions of other people is a very educational experience as well. In recent years I rarely had time to play with puzzles, but this year I’ll try to find a bit of time for Advent of Code.]]></summary></entry></feed>