<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ben Clark</title>
    <description>The latest articles on DEV Community by Ben Clark (@attackmonkey).</description>
    <link>https://dev.to/attackmonkey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F625007%2Fcdcc2c5a-c660-4d40-b15f-9bab8014282c.png</url>
      <title>DEV Community: Ben Clark</title>
      <link>https://dev.to/attackmonkey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/attackmonkey"/>
    <language>en</language>
    <item>
      <title>ReScript and Classless Coding</title>
      <dc:creator>Ben Clark</dc:creator>
      <pubDate>Mon, 21 Jun 2021 19:40:50 +0000</pubDate>
      <link>https://dev.to/attackmonkey/rescript-and-classless-coding-4ghg</link>
      <guid>https://dev.to/attackmonkey/rescript-and-classless-coding-4ghg</guid>
      <description>&lt;p&gt;ReScript is one of my favorite languages right now. It's an easy-to-grasp functional-first language that transpiles to javascript. Unlike some other transpile-to-js languages, you don't need a jvm / .net runtime to get going. In fact it's faster than typescript to get going...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://rescript-lang.org/docs/manual/latest/installation"&gt;https://rescript-lang.org/docs/manual/latest/installation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's syntax is essentially a subset of javascript with functional elements carefully crafted into it.&lt;/p&gt;

&lt;p&gt;Not only is it easy to use, it makes code far far more predictable and safe.&lt;/p&gt;

&lt;p&gt;ReScript however, doesn't have or need classes / prototypes.&lt;/p&gt;

&lt;p&gt;In this post I want to show you how you can easily work in this classless paradigm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Separate state from function
&lt;/h2&gt;

&lt;p&gt;Firstly it helps to separate the concept of state from function.&lt;/p&gt;

&lt;p&gt;In our example, we're going to create an instance of a person. This is done with a &lt;code&gt;type&lt;/code&gt; and a &lt;code&gt;let&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rescript"&gt;&lt;code&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;: &lt;span class="kt"&gt;string&lt;/span&gt;,
  &lt;span class="n"&gt;age&lt;/span&gt;: &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;: &lt;span class="s2"&gt;"Bob"&lt;/span&gt;,
  &lt;span class="n"&gt;age&lt;/span&gt;: &lt;span class="mi"&gt;28&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above, since there is a type that matches bob's signature, bob's type is inferred to be person. We could have declared bob explicitly with &lt;code&gt;let bob: person = { ... }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we have our state, we can think about function(s)...&lt;/p&gt;

&lt;h2&gt;
  
  
  Group functions into modules
&lt;/h2&gt;

&lt;p&gt;It's common to group functions that work on the same type of data into a common module. This is somewhat similar to methods within a class. In the below module we have a &lt;code&gt;greet&lt;/code&gt; and a &lt;code&gt;tellAge&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;Again, note that we haven't had to tell the functions that &lt;code&gt;thisPerson&lt;/code&gt; is of type &lt;code&gt;person&lt;/code&gt; because it is able to infer this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rescript"&gt;&lt;code&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thisPerson&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;thisPerson&lt;/span&gt;.&lt;span class="n"&gt;name&lt;/span&gt;
      &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="s2"&gt;" says Hello."&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nn"&gt;Js&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
    &lt;span class="n"&gt;thisPerson&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;tellAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisPerson&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nn"&gt;Belt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;
    &lt;span class="n"&gt;thisPerson&lt;/span&gt;
      &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;.&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="s2"&gt;" is "&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;.&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="s2"&gt;" years old"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nn"&gt;Js&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
    &lt;span class="n"&gt;thisPerson&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In ReScript you will often see the &lt;code&gt;-&amp;gt;&lt;/code&gt; operator which allows you to "pipe" the previous value into the following function. For example &lt;code&gt;10-&amp;gt;increment-&amp;gt;increment&lt;/code&gt; is the same as &lt;code&gt;increment(increment(10))&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;ReScript doesn't use the &lt;code&gt;return&lt;/code&gt; keyword, but rather returns the last expression in the function. Both of our functions return &lt;code&gt;thisPerson&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Putting it together
&lt;/h2&gt;

&lt;p&gt;So now we "pipe" bob into one of the functions... let's say &lt;code&gt;greet&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rescript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// Note: In ReScript, a top-level expression must always be `unit`. &lt;/span&gt;
&lt;span class="c1"&gt;// `unit` is very similar to `undefined` is javascript.&lt;/span&gt;
&lt;span class="c1"&gt;// Since `bob-&amp;gt;Person.greet` returns a type of `person` we use `ignore` to ignore this type and just return `unit` instead.&lt;/span&gt;

&lt;span class="n"&gt;bob&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nn"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ignore&lt;/span&gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;Person.greet&lt;/code&gt; returns back &lt;code&gt;bob&lt;/code&gt;, we can then continue to pipe into other &lt;code&gt;Person&lt;/code&gt; functions...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rescript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// Using open allows us to drop the need to write `Person.greet` and `Person.tellAge` and just use `greet` and `tellAge`&lt;/span&gt;

&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; 

&lt;span class="n"&gt;bob&lt;/span&gt;
  &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;
  &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tellAge&lt;/span&gt;
  &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ignore&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how we can use the &lt;code&gt;-&amp;gt;&lt;/code&gt; a bit like method chaining in OOP.&lt;/p&gt;

&lt;p&gt;One cool thing with this style of syntax is that &lt;code&gt;bob&lt;/code&gt; doesn't have to be piped into a function from the &lt;code&gt;Person&lt;/code&gt; module and in fact can be piped into any function that accepts the signature.&lt;/p&gt;

&lt;p&gt;For example, lets create a standalone function called &lt;code&gt;incrementAge&lt;/code&gt;...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rescript"&gt;&lt;code&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;incrementAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thisPerson&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;: &lt;span class="n"&gt;thisPerson&lt;/span&gt;.&lt;span class="n"&gt;name&lt;/span&gt;,
  &lt;span class="n"&gt;age&lt;/span&gt;: &lt;span class="n"&gt;thisPerson&lt;/span&gt;.&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;

&lt;span class="n"&gt;bob&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;incrementAge&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tellAge&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ignore&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we run the program it prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Bob says Hello.
Bob is 29 years old

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Immutable-first
&lt;/h2&gt;

&lt;p&gt;You may have noticed that &lt;code&gt;incrementAge&lt;/code&gt; did not mutate &lt;code&gt;bob&lt;/code&gt;, but rather immutably produced a new version of &lt;code&gt;bob&lt;/code&gt; to continue passing through the pipe. This illustrates an important part of functional programming, in that where ever possible, the best approach is to use pure functions like this, that don't mutate existing values. We could then for example keep a current version of &lt;code&gt;bob&lt;/code&gt; and &lt;code&gt;bob1YearFromNow&lt;/code&gt;...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rescript"&gt;&lt;code&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;bob1YearFromNow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bob&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;incrementAge&lt;/span&gt;

&lt;span class="n"&gt;bob&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tellAge&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ignore&lt;/span&gt;

&lt;span class="n"&gt;bob1YearFromNow&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tellAge&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ignore&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Mutating Props
&lt;/h2&gt;

&lt;p&gt;A good 90+ % of our code should be immutable, but what about when we just want to emulate a class and mutate some props! We can do that like so...&lt;/p&gt;

&lt;p&gt;Firstly the &lt;code&gt;person&lt;/code&gt; type will need to explicitly call out that a particular property is mutable (Since everything is immutable by default). From there, we can create a function that accepts a &lt;code&gt;person&lt;/code&gt; and mutate the &lt;code&gt;age&lt;/code&gt; property. Again, we pass back &lt;code&gt;thisPerson&lt;/code&gt;, so that piping can continue should it need to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rescript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// Updating person to have a mutable age&lt;/span&gt;
&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;: &lt;span class="kt"&gt;string&lt;/span&gt;,
  &lt;span class="k"&gt;mutable&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;: &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;jill&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;: &lt;span class="s2"&gt;"Jill"&lt;/span&gt;,
  &lt;span class="n"&gt;age&lt;/span&gt;: &lt;span class="mi"&gt;26&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;mutIncrementAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thisPerson&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;thisPerson&lt;/span&gt;.&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thisPerson&lt;/span&gt;.&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="n"&gt;thisPerson&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;jill&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;mutIncrementAge&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ignore&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So now we have seen how it's possible to emulate class-like behavior in ReScript, however when it comes to mutation - you will rarely see the mutation of single props like above. Mutation usually happens as immutably as possible in a functional-first language. That however sounds like a &lt;strong&gt;part 2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Have you used ReScript?&lt;br&gt;
How do you use a classless language?&lt;/p&gt;

&lt;p&gt;Thanks all :)&lt;/p&gt;

</description>
      <category>rescript</category>
      <category>javascript</category>
      <category>class</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
