<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Darius Foo on Medium]]></title>
        <description><![CDATA[Stories by Darius Foo on Medium]]></description>
        <link>https://medium.com/@dariusf?source=rss-866f03b394b5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Darius Foo on Medium</title>
            <link>https://medium.com/@dariusf?source=rss-866f03b394b5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 07:32:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dariusf/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Reason for OCaml developers]]></title>
            <link>https://medium.com/@dariusf/reason-for-ocaml-developers-39fa371d6136?source=rss-866f03b394b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/39fa371d6136</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[reasonml]]></category>
            <category><![CDATA[ocaml]]></category>
            <dc:creator><![CDATA[Darius Foo]]></dc:creator>
            <pubDate>Fri, 16 Mar 2018 08:14:53 GMT</pubDate>
            <atom:updated>2018-03-16T08:14:53.116Z</atom:updated>
            <content:encoded><![CDATA[<p>Reason is an alternative front end for OCaml, much like the <a href="https://caml.inria.fr/pub/docs/manual-camlp4/manual007.html">revised syntax</a>.</p><p>We’ll go over what’s new, focusing on how Reason improves the OCaml ecosystem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*ggN9wpVUhvz9PU-v8eAhvw.png" /></figure><h3>Syntax</h3><p>Reason is primarily a new look for OCaml: it renders OCaml source in a JavaScript-like syntax.</p><p>The merits of this have been debated in other places, so we’ll focus only on objective improvements. The main one is that a number of OCaml’s syntactic warts have been fixed; for example, <a href="https://en.wikipedia.org/wiki/Dangling_else">nested matches</a>:</p><pre>match xs with<br>| x :: xs -&gt;<br>  match ys with<br>  | [] -&gt; []<br>  | y :: ys -&gt; (x, y)<br>| [] -&gt; [] (* which match does this branch belong to? *)</pre><p>The last line is ambiguous without parenthesizing further, and is parsed as belonging to the closest match. We must use begin/end or parentheses to disambiguate; the former is itself considered <a href="https://stackoverflow.com/a/9936801">a bit of a wart</a>, but works equally well here.</p><pre>match xs with<br>| x :: xs -&gt;<br>  begin match ys with<br>  | [] -&gt; []<br>  | y :: ys -&gt; (x, y)<br>  end<br>| [] -&gt; []</pre><pre>match xs with<br>| x :: xs -&gt;<br>  (match ys with<br>  | [] -&gt; []<br>  | y :: ys -&gt; (x, y))<br>| [] -&gt; []</pre><p>Reason addresses this problem by requiring blocks to be delimited, and choosing relatively terse and familiar tokens for the purpose. Note the splat syntax for destructuring a cons list.</p><pre>switch (xs) {<br>  | [x, ...xs] =&gt;<br>    switch (x) {<br>      | [] =&gt;<br>      | [y, ...ys] =&gt; (x, y)<br>    }<br>  | [] =&gt; []<br>}</pre><p>if statements are given the same treatment. The following is a common construction when writing imperative code, but fails to type-check:</p><pre>if condition then<br>  1<br>else<br>  print_endline &quot;else branch&quot;;<br>  2<br>(*<br>  Error: This expression has type unit but an expression was expected of type int<br>*)</pre><p>The semicolon binds more loosely than most would intuitively expect. This expression is parsed as:</p><pre>(if condition then<br>  1<br>else<br>  print_endline &quot;else branch&quot;);<br>  2</pre><p>… which is the sort of thing one has to trip over repeatedly to learn.</p><p>Again, Reason’s solution is to have opening and closing delimiters.</p><pre>if (condition) {<br>  1<br>} else {<br>  print_endline &quot;else branch&quot;;<br>  2<br>}</pre><p>The double semicolon has also been removed. It’s arguably one of the more obvious warts, as one immediately encounters it when trying out OCaml’s REPL, where it <a href="https://discuss.ocaml.org/t/double-semicolon-peculiarity/1261/7">has a subtly different meaning</a> from when it appears in a source file.</p><p>The tradeoff is that every declaration is ended by a semicolon. This is a little more verbose, but it unifies the <em>four</em> delimiters OCaml uses: ;, ;;, in, or nothing. One might argue that OCaml&#39;s ; communicates imperative semantics, while the other delimiters only serve a syntactic function, and thus they should be kept separate. That is a legitimate argument as well; Reason seems to have decided in favor of overloading ; to keep the language smaller, and closer to JS&#39; imperative-by-default semantics.</p><p>Function arguments must be parenthesized and separated by commas. Whether the increased verbosity in the 1–2 argument cases is worth it and whether it makes sense with currying have already been <a href="https://discuss.ocaml.org/t/reason-general-function-syntax-discussion/537">debated at length</a>, so I’ll just comment on what it improves: functions can no longer be accidentally applied to too many arguments due to missing semicolons/parentheses, so the type error that results from that should no longer occur.</p><p>A few other oddities have been fixed, like type parameters appearing before constructors:</p><pre>type list(&#39;a) =<br>  | Nil<br>  | Cons(&#39;a, list(&#39;a));</pre><p>instead of</p><pre>type &#39;a list =<br>  | Nil<br>  | Cons of &#39;a * &#39;a list</pre><p>SML shares this reversed syntax, but other ML derivatives like Coq’s vernacular language, F#, F*, and Haskell derivatives do not.</p><p>Block comments now look like C’s, so parenthesizing the multiplication operator isn’t as awkward:</p><pre>/* Reason block comment */<br>let a = (*);</pre><pre>(* OCaml; spaces inside the parens are necessary *)<br>let a = ( * )</pre><p>Product types are written like their term-level counterparts: (int, float) instead of int * float.</p><p>It’s notable that a number of these changes also appear in the revised syntax, in spirit if not in form.</p><h3>Tooling</h3><p>The most basic way to use Reason is <a href="https://github.com/reasonml/reason-cli">refmt</a>, a parser/pretty-printer that converts both from and to OCaml syntax. It&#39;s easy to use, but is usually invoked by (and bundled with) higher-level tools.</p><pre>$ refmt a.ml &gt; a.re<br>$ refmt --print ml a.re &gt; a1.ml</pre><p>There’s also rtop, which is utop with the new parser.</p><p>To compile to JS, Reason uses BuckleScript, an OCaml-to-JS compiler. BuckleScript provides bsc and bsrefmt, compilers with corresponding front ends, and bsb, a repackaged <a href="https://ninja-build.org/">Ninja</a> with rules for handling OCaml/Reason sources. Thus, bsb is the simplest high-level way to interact with all of this and target JS, the rough equivalent of ocamlbuild or dune.</p><h3>npm and OPAM</h3><p><a href="https://redex.github.io/">redex</a> is the equivalent of the <a href="https://opam.ocaml.org/packages/">OPAM repository</a>. It is backed by npm, so if you’re targeting only JS and using only npm packages, it should be sufficient to npm install them, then add them to BuckleScript&#39;s configuration file, bsconfig.json. There are a couple of <a href="https://stackoverflow.com/a/46012995">extra steps</a>, but that&#39;s the basic workflow.</p><p><a href="http://esy.sh/">esy</a> is sort of like the Reason <a href="http://esy.sh/docs/en/what-why.html">version</a> of opam for targeting JS: it installs and manages OCaml compilers, eases the use of OPAM packages (which need to be compiled with BuckleScript), and is <a href="https://github.com/esy-ocaml/esy-reason-project/blob/master/package.json">agnostic to build system</a>. esy [command] is the rough equivalent of the familiar eval `opam config env`; [command].</p><h3>Going cross-platform</h3><p><a href="https://github.com/bsansouci/bsb-native">bsb-native</a> is a fork of bsb that supports native compilation. It&#39;s intended for <a href="https://jaredforsyth.com/posts/making-a-cross-platform-mobile-game-in-reason-ocaml/">cross-platform development</a>: compiling to both JS and native targets from the same codebase.</p><p><a href="https://github.com/Schmavery/reprocessing/">reprocessing</a>, a Reason port of <a href="https://processing.org/">Processing</a>, is the de facto cross-platform ‘framework’. It has fancy development features like the use of Dynlink for hot reloading. <a href="https://github.com/jaredly/reprocessing-scripts">rsb</a> <a href="https://jaredforsyth.com/posts/reason-mobile-cross-compilation-deep-dive/">automates the cross-platform aspects</a> and <a href="https://jaredforsyth.com/posts/hot-reloading-ocaml-on-web-desktop-and-android/">takes hot reloading further</a>.</p><p>There is a lot of additional complexity to be ironed out here (notably, around managing dependencies), but the cross-platform work adds significantly to the OCaml ecosystem and takes it in a promising new direction.</p><h3>Development</h3><p>Most of the other OCaml tools have Reason equivalents. An alternate version of Merlin, ocamlmerlin-reason, comes with the Reason CLI package. There is a <a href="https://github.com/reasonml/red">debugger</a> with a nicer CLI interface available. dune works with Reason files out of the box.</p><p><a href="https://blog.hackages.io/reasonml-ppx-8ecd663d5640">BuckleScript works with ppx processors</a>, so things like ppx_deriving could conceptually be used with esy.</p><p>Also notable is a project to <a href="https://github.com/esy-ocaml/BetterErrors">prettify OCaml’s error messages</a>, which is intended for use in esy, but is also useful when writing OCaml.</p><h3>Notable libraries</h3><p>reprocessing <a href="https://github.com/bsansouci/reprocessing-example">enables</a> a whole new domain of applications that OCaml may be used in. It is built on <a href="https://github.com/bsansouci/reasongl">reasongl</a>, which abstracts over WebGL and OpenGL with a common interface.</p><p><a href="https://github.com/aantron/repromise">repromise</a> is a similar abstraction which unifies lwt and JS’ native promises.</p><p>GADTs can be used effectively to <a href="https://andreas.github.io/2017/11/29/type-safe-graphql-with-ocaml-part-1/">model GraphQL types</a>, and there are a number of <a href="https://github.com/mhallin/graphql_ppx">Reason</a> <a href="https://redex.github.io/package/regql">implementations</a> of GraphQL in development. OCaml’s strongly-typed nature works particularly well for this sort of thing.</p><p>Of course, one has to mention React here; Reason’s bindings have <a href="https://www.reactiflux.com/transcripts/jordan-walke/">official support</a> and <a href="https://twitter.com/jordwalke/status/962869115080724480">streamline the use of things like Redux and routers</a>.</p><h3>Conclusion</h3><p>A common response to Reason I’ve seen is that it fragments the OCaml ecosystem: documentation is written in two languages instead of one, people reinvent OCaml libraries in Reason, and tooling ultimately becomes incompatible. This is a bit of stretch to me: <strong>as long as Reason remains a front end for the OCaml compiler, the Reason ecosystem only adds to OCaml’s</strong>.</p><p>In other words, if you’re a seasoned OCaml developer, you can continue to write OCaml and still benefit from the things others are building; the best example is reprocessing and the <a href="https://github.com/jaredly/gravitron">cross-platform apps</a> built with it, where you can just as well use OCaml. OCaml was able to cross-compile before, but things were never so accessible for people who just want to build apps in a nice language without having to wrestle with low-level tools.</p><p>Reason itself is a great step forward for making OCaml’s ecosystem available to more people, and pushing it into new areas, like games and web development.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39fa371d6136" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>