<?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>Forem: Dominik Liebler</title>
    <description>The latest articles on Forem by Dominik Liebler (@domnikl).</description>
    <link>https://forem.com/domnikl</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%2F30628%2F94a1e357-142d-40a0-b14b-fbab8106ff62.jpeg</url>
      <title>Forem: Dominik Liebler</title>
      <link>https://forem.com/domnikl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/domnikl"/>
    <language>en</language>
    <item>
      <title>GitOps for Confluent Schema Registry</title>
      <dc:creator>Dominik Liebler</dc:creator>
      <pubDate>Tue, 01 Jun 2021 12:28:42 +0000</pubDate>
      <link>https://forem.com/domnikl/gitops-for-confluent-schema-registry-n9h</link>
      <guid>https://forem.com/domnikl/gitops-for-confluent-schema-registry-n9h</guid>
      <description>&lt;p&gt;If you are like me, you love version control. Not so much the CLI interface (git isn't very beginner-friendly after all), but the concept of having a changelog of what has been done and - if you use meaningful commit messages - also the reasons behind a change.&lt;/p&gt;

&lt;p&gt;Another thing that I love is Apache Kafka and that it is serialization-format agnostic. However most of the time, I use Avro-serialization because having a schema does bring a lot of pros to the table: it provides a safe way to change the contracts producers and consumers share and we all know:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Change is inevitable in life. - Jack Canfield&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Schema evolutions and registry
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.confluent.io/platform/current/schema-registry/index.html" rel="noopener noreferrer"&gt;Confluent's Schema Registry&lt;/a&gt; provides a central storage and retrieval API for schemas to be used by Kafka consumers and procuders. It also checks compatibility for schema evolutions to enable changes be backward- and/or forward-compatible.&lt;/p&gt;

&lt;p&gt;Schema evolutions are often performed automatically when producers change the schema they use to write. This may be fine for prototyping but can lead to serious issues in production: in case of an error in the schema a rollback might not be as easy as going back to the previous version as eg. a newly-introduced field cannot be removed as easily and might need a lot of manual intervention to get back to the previous state. Also if the schema evolution fails because of compatibility issues, it will fail only when the changes are deployed and the new code runs for the first time. This one can be mitigated by testing on another environment first (which you should do regardless of this problem), but still.&lt;/p&gt;

&lt;h2&gt;
  
  
  Open Source ftw!
&lt;/h2&gt;

&lt;p&gt;Now I wanted to combine both technologies and open sourced &lt;a href="https://github.com/domnikl/schema-registry-gitops" rel="noopener noreferrer"&gt;schema-registry-gitops&lt;/a&gt; to prevent from the above issues: having a version-controlled history of schema changes and push them to the registry only when ready and reviewed in QA. It can be used in CI/CD pipelines to ensure that schema changes are compatible with previous versions and can be part of your code review process.&lt;/p&gt;

&lt;p&gt;My team at &lt;a href="https://www.flyeralarm.com/" rel="noopener noreferrer"&gt;FLYERALARM&lt;/a&gt; uses it in our Atlassian Bamboo Pipelines to ensure that no bad schema evolutions make it into production and we've had no issues since! It's written in Kotlin using the APIs that Confluent provides for Schema Registry and Avro-serialization.&lt;/p&gt;

</description>
      <category>kafka</category>
      <category>schemaregistry</category>
      <category>gitops</category>
    </item>
    <item>
      <title>Scoped CSS for React Components with TypeScript</title>
      <dc:creator>Dominik Liebler</dc:creator>
      <pubDate>Tue, 25 May 2021 08:17:54 +0000</pubDate>
      <link>https://forem.com/domnikl/scoped-css-for-react-components-with-typescript-31a4</link>
      <guid>https://forem.com/domnikl/scoped-css-for-react-components-with-typescript-31a4</guid>
      <description>&lt;p&gt;React applications are being composed of small components that can be used individually and in best case scenario can be reused across multiple applications. But what about the CSS that they need for layouting elements inside them?&lt;/p&gt;

&lt;p&gt;Often you end up inventing a system for it, something like that: you group CSS rules by a class name for each component and everything that is specific to a component goes in there. It's a start but it's not perfect. Soon you start renaming stuff or you want to apply styles from a global perspective.&lt;/p&gt;

&lt;p&gt;Coming from the Vue.js world, I especially liked the approach of &lt;a href="https://vuejs.org/v2/guide/single-file-components.html" rel="noopener noreferrer"&gt;Single File Components&lt;/a&gt; - everything that belongs to that component goes into one file, CSS, HTML and JavaScript or TypeScript and I wanted to have this in React too, so I took of into the world of CSS-in-JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Modules
&lt;/h2&gt;

&lt;p&gt;I came to React, looking for Single File Components all over the place and as it turns out, it's not that easy 😔 What I did find though, is &lt;a href="https://github.com/css-modules/css-modules" rel="noopener noreferrer"&gt;CSS Modules&lt;/a&gt; 🥳&lt;br&gt;
It works like this: you import the CSS as a JavaScript module , which have been mapped from your CSS class names and assign those as &lt;code&gt;className&lt;/code&gt; properties in the JSX. I used the npm package &lt;code&gt;typescript-plugin-css-modules&lt;/code&gt; for this. This is how a Component styled with it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./foo.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FooComponent&lt;/span&gt; &lt;span class="o"&gt;=&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;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myClassName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, World!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I still wasn't satisfied with this approach as there were still two files to edit when the component needs to be modified. Then I learned about &lt;a href="https://emotion.sh/" rel="noopener noreferrer"&gt;Emotion&lt;/a&gt; in &lt;a href="https://twitter.com/jlengstorf?" rel="noopener noreferrer"&gt;Jason Lengstorf&lt;/a&gt;'s &lt;a href="https://frontendmasters.com/courses/gatsby/" rel="noopener noreferrer"&gt;Introduction to Gatsby&lt;/a&gt; course on Frontend Masters and it was exactly what I was looking for. I was intruiged 😁&lt;/p&gt;

&lt;h2&gt;
  
  
  Emotion to the rescue
&lt;/h2&gt;

&lt;p&gt;To style React components with emotion, there are several options to choose from depending on your preferences and what you want to achieve. The one I like most as a starting point, is using the &lt;code&gt;css&lt;/code&gt;-template string helper that lets you write CSS like you would in a CSS file. This is a sample component using it to set a &lt;code&gt;width&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt; and &lt;code&gt;background-color&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** @jsxImportSource @emotion/react */&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@emotion/react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;
      &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="s2"&gt;`
        width: 100px;
        height: 100px;
        background-color: red;
      `&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      using the css template string helper
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Card&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Really simple, huh? The first line &lt;code&gt;/** @jsxImportSource @emotion/react */&lt;/code&gt; tells the TypeScript compiler, how to resolve the css helper and it took me quite a while to figure it out!&lt;/p&gt;

&lt;p&gt;But it can get quite messy when the component grows and contains more tags than just this one div. For this occasion, you can refactor the component and use styled components like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@emotion/styled&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ListItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;`
  font-weight: bold;
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ListProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;List&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;ListProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ListItem&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ListItem&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;List&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, &lt;code&gt;ListItem&lt;/code&gt; uses the &lt;code&gt;styled&lt;/code&gt; function to create a styled component that only adds CSS to a &lt;code&gt;li&lt;/code&gt; element and automatically renders its children in it.&lt;/p&gt;

&lt;p&gt;Now I got to the point where I was satisfied. At least until I realized that I wanted to have kind of like a theming, where I would store colors, definitions of borders and such things that I would need over and over again in a central location. Emotion provides a Theming API, why not try that?&lt;/p&gt;

&lt;h2&gt;
  
  
  Theming
&lt;/h2&gt;

&lt;p&gt;To start with the theme, I implemented a new class for it and created a new instance of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Theme&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;primaryColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Theme&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Theming then works like this: you provide the Theme to your components using a &lt;code&gt;&amp;lt;ThemeProvider&amp;gt;&lt;/code&gt; and access the theme in the actual component using the &lt;code&gt;useTheme&lt;/code&gt; hook provided by &lt;code&gt;@emotion/react&lt;/code&gt;. Here's my App, that does exactly that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ThemeProvider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@emotion/react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ThemeProvider&lt;/span&gt; &lt;span class="na"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MyComponentWithTheme&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ThemeProvider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;And here is &lt;code&gt;MyComponentWithTheme&lt;/code&gt;, which uses both the Theme and the &lt;code&gt;css&lt;/code&gt; template string helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** @jsxImportSource @emotion/react */&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;css&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@emotion/react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useTheme&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@emotion/react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Theme&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ComponentWithTheme&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;theme&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useTheme&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Theme&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;
      &lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="s2"&gt;`
        width: 100px;
        height: 100px;
        background: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;theme&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;primaryColor&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
      `&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      Component using the Theme provided in App
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ComponentWithTheme&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using that, I found a way to write the CSS I need for my components directly in the components. I still need to figure out what parts of the style sheets go into a component or into a global style sheet, but it's a start.&lt;/p&gt;

&lt;p&gt;Of course emotion does a lot more than that (like adding vendor prefixes and stuff) but I am still learning about it and find it very interesting and fun. I am looking forward for your tips and tricks around emotion and CSS-in-JS in common!&lt;/p&gt;

</description>
      <category>react</category>
      <category>css</category>
    </item>
    <item>
      <title>Using actors in Kotlin - a concurrent play in one act</title>
      <dc:creator>Dominik Liebler</dc:creator>
      <pubDate>Mon, 16 Sep 2019 00:00:00 +0000</pubDate>
      <link>https://forem.com/domnikl/using-actors-in-kotlin-a-concurrent-play-in-one-act-3hn5</link>
      <guid>https://forem.com/domnikl/using-actors-in-kotlin-a-concurrent-play-in-one-act-3hn5</guid>
      <description>&lt;p&gt;In february this year I already wrote about &lt;a href="https://dev.to/domnikl/kotlin-coroutines-and-javafx-threads-26od/"&gt;Kotlin coroutines and their usage in JavaFX applications&lt;/a&gt; and today I want to explore coroutines even further and talk about them in the context of actors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not Threads?
&lt;/h2&gt;

&lt;p&gt;A modern software system has to manage a lot of different tasks at the same time. User requests coming in via web interfaces, GUIs and other channels need to be handled while there is background work to be done. Traditionally this has been the domain of operation system threads that the application spawned and provided with work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F9xbs2na48kvzncjsh9cd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F9xbs2na48kvzncjsh9cd.jpg" title="Photo by Héctor J. Rivas on Unsplash" alt="Photo by Héctor J. Rivas on Unsplash" width="800" height="533"&gt;&lt;/a&gt;Photo by Héctor J. Rivas&lt;/p&gt;

&lt;p&gt;But as you may know, threading is error-prone and not easily done right when it comes to mutating state in an complex application (if you don’t you should read &lt;a href="https://web.stanford.edu/~ouster/cgi-bin/papers/threads.pdf" rel="noopener noreferrer"&gt;Why Threads Are A Bad Idea (for most purposes)&lt;/a&gt;). With &lt;code&gt;coroutines&lt;/code&gt;, there is a lightweight and simple alternative to threads in Kotlin that follows the approach of &lt;a href="https://de.wikipedia.org/wiki/Communicating_Sequential_Processes" rel="noopener noreferrer"&gt;Communicating Sequential Processes&lt;/a&gt;. It’s the same idea that inspired Go’s goroutines:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do not communicate by sharing memory; instead, share memory by communicating. - &lt;a href="https://golang.org/doc/effective_go.html#sharing" rel="noopener noreferrer"&gt;Effective Go&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kotlin achieves that by providing the concept of a &lt;code&gt;Channel&lt;/code&gt;, which basically is a &lt;a href="https://kotlinlang.org/docs/reference/coroutines/channels.html#channel-basics" rel="noopener noreferrer"&gt;Queue that uses suspending functions&lt;/a&gt;. Using coroutines and channels, we can build a system that encapsulate mutable state in a manner that do not need any locks and synchronization and instead leverage a protocol of messages to handle concurrent updates of that state.&lt;/p&gt;

&lt;h2&gt;
  
  
  The actor model
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fikohisoqozn1bfpcrwtg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fikohisoqozn1bfpcrwtg.jpg" title="Photo by Avel Chuklanov on Unsplash" alt="Photo by Avel Chuklanov on Unsplash" width="800" height="533"&gt;&lt;/a&gt;Photo by Avel Chuklanov&lt;/p&gt;

&lt;p&gt;Such a model is called an actor. It is not a new concept, instead it has been around for years and is the underlying concept of Erlang and can also be used in Java and Scala using Akka. In &lt;a href="https://domnikl.github.io/2018/03/how-does-keeping-state-in-elixir-work/" rel="noopener noreferrer"&gt;How does keeing state in Elixir work?&lt;/a&gt; I built an actor in Elixir even though the concept is not officially called an ‘actor’ in the BEAM world, but &lt;code&gt;gen_server&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;Actors come in many sizes and they can do a wide variety of tasks, but they all share the same properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they keep internal state&lt;/li&gt;
&lt;li&gt;they are running concurrently&lt;/li&gt;
&lt;li&gt;their state can be manipulated through messages only&lt;/li&gt;
&lt;li&gt;they receive messages in a channel that is called a ‘mail box’&lt;/li&gt;
&lt;li&gt;they process messages sequentially&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Don’t try this at home!
&lt;/h2&gt;

&lt;p&gt;Ok, this might be a bit exaggerated, but there is a need to warn you. Precisely to warn you about the state of implementation for the &lt;code&gt;actor()&lt;/code&gt; function. As of writing it is a very simple API to create an actor but this is about to change in &lt;a href="https://github.com/Kotlin/kotlinx.coroutines/issues/87" rel="noopener noreferrer"&gt;future releases of kotlinx.coroutines&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  basic counter example
&lt;/h2&gt;

&lt;p&gt;So what does an actor in Kotlin look like? Let’s have a look.&lt;/p&gt;

&lt;p&gt;First of all, we need to define messages we can send to the actor. In this simple example, the actor stores a counter that can be incremented by arbitrary integer values using &lt;code&gt;Message.Increment&lt;/code&gt; and the current value can be requested by &lt;code&gt;Message.GetValue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To send data back to the requesting code, we use a &lt;code&gt;CompletableDeferred&lt;/code&gt; here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;deferred&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CompletableDeferred&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Message&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;Next up, we define the actor named &lt;code&gt;basicActor&lt;/code&gt; itself. It needs to be defined as an extension function to &lt;code&gt;CoroutineScope&lt;/code&gt; as it will launch a coroutine to run in. It runs a simple &lt;code&gt;for&lt;/code&gt; loop to get messages from the channel that will run until anyone closes the channel. The handling of the messages is rather straight forward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;basicActor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Increment&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetValue&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deferred&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&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;That’s it! But to use it, you also have to define a client that sends messages to the actor. Here’s the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runBlocking&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;channel&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;basicActor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;deferred&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CompletableDeferred&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;

    &lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deferred&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deferred&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;await&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// prints "3"&lt;/span&gt;

    &lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&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 &lt;code&gt;main&lt;/code&gt; we define a &lt;code&gt;CoroutineScope&lt;/code&gt; by using &lt;code&gt;runBlocking()&lt;/code&gt; which is then used to launch the actor in and send messages. &lt;code&gt;channel.close()&lt;/code&gt; will then cause the actor to complete. Without it, the actor will keep the program running infinitely, which may be a desired state in a typically server situation.&lt;/p&gt;

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

&lt;p&gt;Actors are an easy concept that can help to remove the hassle of concurrent computing. They are still experimental in &lt;code&gt;kotlinx.coroutines&lt;/code&gt; but it’s a first step in the direction and I am curious of the developments and as a long-time fan of Erlang and Elixir I appreciate actors and would happily welcome them as a stable component of Kotlin coroutines.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>coroutines</category>
      <category>actors</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>Kotlin coroutines and JavaFX threads</title>
      <dc:creator>Dominik Liebler</dc:creator>
      <pubDate>Sat, 02 Feb 2019 00:00:00 +0000</pubDate>
      <link>https://forem.com/domnikl/kotlin-coroutines-and-javafx-threads-26od</link>
      <guid>https://forem.com/domnikl/kotlin-coroutines-and-javafx-threads-26od</guid>
      <description>&lt;p&gt;On October 29, 2018 JetBrains released a &lt;a href="https://blog.jetbrains.com/kotlin/2018/10/kotlin-1-3/" rel="noopener noreferrer"&gt;new version of Kotlin (1.3)&lt;/a&gt; that included a long-awaited feature of the language: coroutines. Contrary to Go’s &lt;code&gt;goroutine&lt;/code&gt; construct, they are not implemented as a language feature. Instead they are developed as a pure library implementation written in Kotlin. But apart from that they share a lot of similiarites with goroutines.&lt;/p&gt;

&lt;p&gt;Behind the scenes, coroutines are very lightweight because they are being multiplexed on a few threads and creating one is much faster than spawning a new thread.&lt;/p&gt;

&lt;p&gt;You can learn more about them over at JetBrains’ tutorial: &lt;a href="https://kotlinlang.org/docs/tutorials/coroutines/coroutines-basic-jvm.html" rel="noopener noreferrer"&gt;“Your first coroutine with Kotlin”&lt;/a&gt;. Here I want to show you the caveat that you might encounter in a JavaFX applications when using coroutines.&lt;/p&gt;

&lt;p&gt;So this is my basic JavaFX application written in Kotlin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyApp&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;button&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;also&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"waiting for 0s"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primaryStage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Stage&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;pane&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pane&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;also&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;children&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;primaryStage&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="n"&gt;scene&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Scene&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pane&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;200.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;200.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;primaryStage&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyApp&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&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;Nothing special here, just a GUI with a label that shows the text &lt;code&gt;waiting for 0s&lt;/code&gt;. Now we want to extend our application to continuously update the label to always tell me the exact time I waited.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MillisElapsedCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GlobalScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;millisElapsed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;millisElapsed&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
                &lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;millisElapsed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;millisElapsed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&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;&lt;strong&gt;Notice that you need to add a dependency to &lt;code&gt;org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1&lt;/code&gt; to be able to run this (remember: it’s just a library!)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So this is the implemention of the &lt;code&gt;MillisElapsedCounter&lt;/code&gt; class and a corresponding &lt;code&gt;Observer&lt;/code&gt; that is being notified every 1000ms. Clients of this class have to instantiate it providing an observer. Calling &lt;code&gt;start()&lt;/code&gt; will then launch a new coroutine using the default dispatcher (there is a pool of dispatchers - which are threads behind the scenes - you can use) and return immediately. Note that &lt;code&gt;delay(1000)&lt;/code&gt; is a &lt;code&gt;suspend&lt;/code&gt; function, it will suspend execution in the thread it runs in. Running that in the application thread would freeze the UI forever and would be a bad idea.&lt;/p&gt;

&lt;p&gt;To use the counter we also need to update &lt;code&gt;MyApp&lt;/code&gt; to implement &lt;code&gt;MillisElapsedCounter.Observer&lt;/code&gt;’s &lt;code&gt;notify(millisElapsed: Int)&lt;/code&gt; and start the counter like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyApp&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;MillisElapsedCounter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Observer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;button&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;also&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"waiting for 0s"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primaryStage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Stage&lt;/span&gt;&lt;span class="p"&gt;?)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;MillisElapsedCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;millisElapsed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"waiting for ${millisElapsed / 1000}s"&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;Now if you run that, it will throw a &lt;code&gt;java.lang.IllegalStateException&lt;/code&gt; exception that is coming from JavaFX:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Exception in thread “DefaultDispatcher-worker-3” java.lang.IllegalStateException: Not on FX application thread; currentThread = DefaultDispatcher-worker-3&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To prevent from concurrency issues in the state of the UI, JavaFX prevents other threads from changing it. Look at the &lt;code&gt;main()&lt;/code&gt; function that uses &lt;code&gt;launch(MyApp::class.java)&lt;/code&gt; to start the application. This starts a new thread called &lt;code&gt;JavaFX-Launcher&lt;/code&gt; which in turn starts the &lt;code&gt;JavaFX Application Thread&lt;/code&gt; which runs all the UI related stuff. This one is the only thread allowed to make changes in the UI at runtime.&lt;/p&gt;

&lt;p&gt;This means that we need to execute the changes that are being made to &lt;code&gt;button.text&lt;/code&gt; in this very thread. The Kotlin team implemented another library for this sole purpose: &lt;strong&gt;org.jetbrains.kotlinx:kotlinx-coroutines-javafx:1.1.1&lt;/strong&gt; provides a new Dispatcher &lt;code&gt;Dispatchers.JavaFx&lt;/code&gt; that uses the JavaFX Application Thread (there’s also &lt;strong&gt;-android&lt;/strong&gt; and &lt;strong&gt;-swing&lt;/strong&gt; to be used correspondingly).&lt;/p&gt;

&lt;p&gt;Then the solution to our problem here is to use the correct Dispatcher &lt;code&gt;Dispatchers.JavaFx&lt;/code&gt; and to implement another interface in &lt;code&gt;MyApp&lt;/code&gt; called &lt;code&gt;CoroutineScope&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyApp&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;MillisElapsedCounter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Observer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;coroutineContext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineContext&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;JavaFx&lt;/span&gt;
        &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As it is now a &lt;code&gt;CoroutineScope&lt;/code&gt; we can launch new coroutines directly from &lt;code&gt;notify()&lt;/code&gt; using the correct scope:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;millisElapsed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"waiting for ${millisElapsed / 1000}s"&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;And that’s it! The exception will no longer occur and everything will run as expected - I waited a long time, and it still counts happily the seconds I waited :)&lt;/p&gt;

&lt;p&gt;One thing we can improve though is that &lt;code&gt;MillisElapsedCounter&lt;/code&gt; runs a coroutine in the &lt;code&gt;GlobalScope&lt;/code&gt; which means that you need to keep track of it’s lifetime (if you were interested in that - eg. stopping it). More details on that in &lt;a href="https://medium.com/@elizarov/the-reason-to-avoid-globalscope-835337445abc" rel="noopener noreferrer"&gt;The reason to avoid GlobalScope&lt;/a&gt;. To fix that we could implement the interface &lt;code&gt;CoroutineScope&lt;/code&gt; in &lt;code&gt;MillisElapsedCounter&lt;/code&gt; as well:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MillisElapsedCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineScope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;coroutineContext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineContext&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We now have two coroutines cleanly separated from each other and each running in it’s own scope. A clean and simple solution I think.&lt;/p&gt;

&lt;p&gt;The complete source code is available on GitHub, so you can clone it and play around with it:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/domnikl" rel="noopener noreferrer"&gt;
        domnikl
      &lt;/a&gt; / &lt;a href="https://github.com/domnikl/kotlin-coroutines-and-javafx-threads" rel="noopener noreferrer"&gt;
        kotlin-coroutines-and-javafx-threads
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Sample code from my blog post
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Kotlin coroutines and JavaFX threads&lt;/h1&gt;

&lt;/div&gt;

&lt;p&gt;This is the example from my &lt;a href="https://domnikl.github.io/2019/02/kotlin-coroutines-and-javafx-threads/" rel="nofollow noopener noreferrer"&gt;blog post with the same title&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/domnikl/kotlin-coroutines-and-javafx-threads/blob/step0/src/main/kotlin/Main.kt" rel="noopener noreferrer"&gt;Step 0&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/domnikl/kotlin-coroutines-and-javafx-threads/blob/step1/src/main/kotlin/Main.kt" rel="noopener noreferrer"&gt;Step 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/domnikl/kotlin-coroutines-and-javafx-threads/blob/step2/src/main/kotlin/Main.kt" rel="noopener noreferrer"&gt;Step 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/domnikl/kotlin-coroutines-and-javafx-threads/blob/step3/src/main/kotlin/Main.kt" rel="noopener noreferrer"&gt;Step 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
  &lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/domnikl/kotlin-coroutines-and-javafx-threads" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;You can read more about that in &lt;a href="https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md#launch-ui-coroutine" rel="noopener noreferrer"&gt;Guide to UI programming with coroutines&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>javafx</category>
      <category>coroutines</category>
      <category>threads</category>
    </item>
    <item>
      <title>My four stages to learn new tech stuff</title>
      <dc:creator>Dominik Liebler</dc:creator>
      <pubDate>Wed, 24 Oct 2018 00:00:00 +0000</pubDate>
      <link>https://forem.com/domnikl/my-four-stages-to-learn-new-tech-stuff-1f0c</link>
      <guid>https://forem.com/domnikl/my-four-stages-to-learn-new-tech-stuff-1f0c</guid>
      <description>&lt;p&gt;Over the last 15 years that I have been a professional software developer now, I learned more than 16 programming languages and several frameworks, database systems and other tools that helped me in my path to master software craftsmanship.&lt;/p&gt;

&lt;p&gt;Some of that I learned because I needed it in a project for a client but some of these technologies I was just curious about. If you are a software developer you know how fast our world moves on to new technology to solve new types of problems with it.&lt;/p&gt;

&lt;p&gt;So you need to get going with new tech fast and know how to learn efficiently. Here’s how I learn today, a play on four stages:&lt;/p&gt;

&lt;h1&gt;
  
  
  Stage 1: syntax/terminology
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NJolYGAl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mi7p8tznirk4fhkvtuf0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NJolYGAl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mi7p8tznirk4fhkvtuf0.jpg" alt="some JavaScript minified code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing you need to learn about a new programming language or tool is syntax and terminology. If you already know another language or tool you learned a certain way to solve problems in this language but now you have to look out how to solve similar problems in this new language and need to learn the concepts behind it.&lt;/p&gt;

&lt;p&gt;To be honest, I learn syntax once and forget about it later anyway. I have to Google that stuff like A LOT. In my regards the best way to learn about it initially is to look out for Koans.&lt;/p&gt;

&lt;p&gt;Koans are small exercises where you have to fix or write small parts of code and often they are backed by unit tests which you have to pass in order to move on to the next one. Have a look at &lt;a href="http://www.lauradhamilton.com/learn-a-new-programming-language-today-with-koans"&gt;Laura Diane Hamilton&lt;/a&gt;’s comprehensive list of Koans for that.&lt;/p&gt;

&lt;p&gt;Most tools come with documentation and contain a simple tutorial to accompany you taking your first steps with it. Eg. if your intend to learn Docker, there is a great example in the &lt;a href="https://docs.docker.com/get-started/"&gt;official Docker documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Third-party docs may be very useful, too. When using Go, I always have to Google stuff like how to let goroutines communicate with another using channels and I always end up using &lt;a href="https://gobyexample.com"&gt;Go by example&lt;/a&gt; which is another great example. A great list for other languages can be found at &lt;a href="https://learnxinyminutes.com/"&gt;Learn X in Y minutes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But most of the time, I don’t set out to learn just the syntax of a new language. I wanna play around and explore. What helps me in this regard is &lt;a href="https://www.codewars.com"&gt;Code Wars&lt;/a&gt;. By solving small problems, it forces me to learn syntax and explore idiomatic ways to solve the problem efficiently.&lt;/p&gt;

&lt;h1&gt;
  
  
  Stage 2: ecosystem
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y3TzNnG2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w1jabhpvpntalo8lur4q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y3TzNnG2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/w1jabhpvpntalo8lur4q.jpg" alt="the ecosystem"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“If I have seen further it is by standing on the shoulders of Giants.” - Isaac Newton&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you know about syntax and terminology, but where to go next? Learning about the tooling and libraries of a new language or database system is very important (and exciting) IMO. No matter if you need logging, recording metrics or a client to connect to a specific database system there are (open source) libraries that you can build on and you need to know them if you want to build something useful with a new programming language.&lt;/p&gt;

&lt;p&gt;Luckily there are so-called “&lt;a href="https://github.com/sindresorhus/awesome"&gt;Awesome Lists&lt;/a&gt;” for that on GitHub that contain everything you need for every language you can imagine.&lt;/p&gt;

&lt;p&gt;At this stage, I try to do something useful with it. That means I can use the tool I build or at least learn something from making it. More often than not I will throw that away afterwards. Don’t ever get too sentimental with code! After all you will write lot’s of it in your career and very few of it will get to production or stay there long.&lt;/p&gt;

&lt;p&gt;So most of the time, I will just rewrite a Unix program in my new language. I have written &lt;code&gt;cat&lt;/code&gt; in C, Go and Kotlin - &lt;code&gt;tail&lt;/code&gt; in Python, PHP and Java and many more. I never use them of course but they were tools I know very well and wich are documented very well so I had a clearly defined goal to go for.&lt;/p&gt;

&lt;p&gt;Apart from that I often try to solve problems I had solved in the past with another language and try to solve it again in a new language in a more simple way, which is a compelling task sometimes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Stage 3: best practices &amp;amp; experience
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TkwtFFCW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bdoqw9uqjg6d6q9xheoa.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TkwtFFCW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/bdoqw9uqjg6d6q9xheoa.jpg" alt="an old wise man"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When it comes to adopting a new language, syntax and libraries aren’t just enough to build stable and reliable software with it that is also maintainable and can be easily extended. You also need to gain experience with that new piece of technology.&lt;/p&gt;

&lt;p&gt;Usually that means to run it in production, get caught up in problems and get to know how to fix them. But there is another, less harmful way to get that by learning from others and by learning about best practices upfront. This stage is all about the idiomatic way to use a language or system.&lt;/p&gt;

&lt;p&gt;There are two main paths for me here: first, there is Google. You can find a lot of content on best practices in (official) documentation or blog posts which can help you discover best practices. And it helps a lot to hear talks at conferences where people are talking about their experience in using that technology.&lt;/p&gt;

&lt;h1&gt;
  
  
  Stage 4: unlearn old concepts and habits
&lt;/h1&gt;

&lt;p&gt;After learning a new tool or language, you shouldn’t forget to unlearn things, too. If you embrace and accept technical change (which I guess you do if you came here), you should also accept that you have to unlearn old habits, patterns and terminology at some point. After all in our industry, there is a lot of change going on all the time and at a very fast pace.&lt;/p&gt;

&lt;p&gt;So to not get confused when learning about new tech and it’s terminology you should simply forget about old tech. An example: “table” must have had another meaning before there were relational database systems and people had to forget about that in the 1970s, when those RDBMS where new and cutting-edge technology.&lt;/p&gt;

&lt;p&gt;That’s my four stages of learning. I don’t think this will be the last iteration of it, as I won’t stop learning and therefor evolving my learning process. I would also be very happy to hear about your learning process for inspiration.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>careerbuilding</category>
      <category>career</category>
    </item>
    <item>
      <title>How does keeping state in Elixir work?</title>
      <dc:creator>Dominik Liebler</dc:creator>
      <pubDate>Tue, 06 Mar 2018 00:00:00 +0000</pubDate>
      <link>https://forem.com/domnikl/how-does-keeping-state-in-elixir-work-1cc</link>
      <guid>https://forem.com/domnikl/how-does-keeping-state-in-elixir-work-1cc</guid>
      <description>&lt;p&gt;In 2011, when I was new to Erlang and functional programming, the biggest question I had was something like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When there is no state in functional programming, how do you store, retrieve and update state?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I read about &lt;a href="http://erlang.org/doc/man/gen_server.html"&gt;gen_server&lt;/a&gt; that really just was the point where I realised that although everyone kept saying “There is no state in functional programming” you actually do have mechanisms to store state in Erlang/Elixir for later use.&lt;/p&gt;

&lt;p&gt;So I dived into it and learned how it’s done.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it’s done
&lt;/h2&gt;

&lt;p&gt;Actually the concept behind it is not so much rocket-science. Basically it consists of a function calling itself recursively and communicating with other processes which want to store and update the state. If the state needs to be changed, the next call will be made with the new state.&lt;/p&gt;

&lt;p&gt;Let’s build a Stack module that holds it’s values and allows users to &lt;code&gt;push&lt;/code&gt; and &lt;code&gt;pop&lt;/code&gt; values from it.&lt;/p&gt;

&lt;p&gt;We start off with a simple function that will run and keep the state by recursively calling itself and receiving basic messages to handle changes to the state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Stack&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;_from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:push&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:pop&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="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;loop/1&lt;/code&gt; waits for new messages to arrive and uses &lt;code&gt;receive&lt;/code&gt; to block until there are messages for this process which will be pattern-machted against. As you can see, there are two patterns for the two operations we support currently.&lt;/p&gt;

&lt;p&gt;The first one will match &lt;code&gt;{_from, :push, value}&lt;/code&gt; which handles pushes on to the stack. The sending pid &lt;code&gt;_from&lt;/code&gt; is not needed as this is an asynchronous operation. It starts the next iteration with the new &lt;code&gt;value&lt;/code&gt; added as the new first element of &lt;code&gt;state&lt;/code&gt; which will again wait for new messages to arrive.&lt;/p&gt;

&lt;p&gt;The other pattern &lt;code&gt;{from, :pop}&lt;/code&gt; handles the pop operation. The message sender &lt;code&gt;from&lt;/code&gt; will receive back a message with the first element of &lt;code&gt;state&lt;/code&gt; extracted while the rest of the list is given to yet another call to &lt;code&gt;loop/1&lt;/code&gt; which will then wait for another message to arrive.&lt;/p&gt;

&lt;h1&gt;
  
  
  Test it
&lt;/h1&gt;

&lt;p&gt;Let’s take our stack implementation for a spin. Start &lt;code&gt;iex&lt;/code&gt;, paste the above module into it and we are ready to go! To start the stack, just spawn the function as a process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:loop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[[]])&lt;/span&gt;
&lt;span class="c1"&gt;#PID&amp;lt;0.108.0&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;spawn/3&lt;/code&gt; expects a module (Stack), the function name as an atom (:loop) and a list of arguments, which in our case is an empty list. This makes the initial call, which is essentially &lt;code&gt;Stack.loop([])&lt;/code&gt;. The returned &lt;code&gt;pid&lt;/code&gt; is a process identifier to which all messages need to be sent.&lt;/p&gt;

&lt;p&gt;To push a new value on to the stack, we need to send that &lt;code&gt;pid&lt;/code&gt; a message like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="ss"&gt;:push&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;#PID&amp;lt;0.88.0&amp;gt;, :push, 1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;self()&lt;/code&gt; returns the value of the current process (which is iex’s &lt;code&gt;pid&lt;/code&gt; in our case) and send will return the message sent. For this operation we don’t need it, but it’s a good practice to send it anyway. Also we don’t see any change to the stack yet, but remember: the push operation is async.&lt;/p&gt;

&lt;p&gt;Now let’s see the change in the stack by popping a value from the stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="ss"&gt;:pop&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="c1"&gt;#PID&amp;lt;0.88.0&amp;gt;, :push, 1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;self()&lt;/code&gt; is required, because we expect a message back to our iex-pid containing the value. Again, &lt;code&gt;send&lt;/code&gt; returns the message sent and nothing else happens. That’s because we didn’t read the message we got back yet. Let’s use the iex helper &lt;code&gt;flush&lt;/code&gt; to retrieve the contents of our main process’ mailbox:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="ss"&gt;:ok&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And there it is; as expected, the stack contained the value &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  a clean client interface
&lt;/h1&gt;

&lt;p&gt;As our stack is now working, we should build a nice, clean API around it. A user that would want to use the stack module should not care about which messages are required to sent to the process. Instead he/she should use only functions to place values on the stack:&lt;/p&gt;

&lt;p&gt;Let’s extend the module and add a bunch of short functions that build up the API and a clean client user interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="k"&gt;defmodule&lt;/span&gt; &lt;span class="no"&gt;Stack&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;start_link&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;spawn_link&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="bp"&gt;__MODULE__&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:loop&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="ss"&gt;:push&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="ss"&gt;:ok&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
        &lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="ss"&gt;:pop&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="k"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you compare this against the callbacks that you would need to implement when using &lt;a href="https://hexdocs.pm/elixir/GenServer.html"&gt;GenServer&lt;/a&gt;, then you will see that it uses the same naming and function signatures. This is not by accident - it is to show you that GenServer does exactly the same as the above code. Except for a little more error handling and in a more generic way of course. But the concepts remain the same.&lt;/p&gt;

&lt;p&gt;Btw. thanks to &lt;a href="http://learnyousomeerlang.com/recursion"&gt;tail call optimization&lt;/a&gt; the call stack of the &lt;code&gt;loop/1&lt;/code&gt; function is constant in terms of memory usage and therefor not a problem when running forever and ever.&lt;/p&gt;

&lt;p&gt;Here I will leave you off to some experimenting with state as messages and recursive functions. Happy Elixir’ing!&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>state</category>
      <category>functional</category>
    </item>
    <item>
      <title>Docker - Standard_init_linux.go:187 exec user process caused no such file or directory</title>
      <dc:creator>Dominik Liebler</dc:creator>
      <pubDate>Wed, 31 Jan 2018 00:00:00 +0000</pubDate>
      <link>https://forem.com/domnikl/docker-standardinitlinux-go-187-exec-user-process-caused-no-such-file-or-directory-4kh8</link>
      <guid>https://forem.com/domnikl/docker-standardinitlinux-go-187-exec-user-process-caused-no-such-file-or-directory-4kh8</guid>
      <description>&lt;p&gt;I have been working a lot with Docker and Go lately and I come across a very annoying error when running tools built in Go in containers on a RaspberryPi. All of the bug reports I found had something to do with Windows-style line endings (CRLF) but I am using MacOS, so this can’t be the solution (at least) for me here.&lt;/p&gt;

&lt;p&gt;This is the Dockerfile I have been using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM alpine
WORKDIR /app
ADD washing-machine /app/washing-machine
CMD ["./washing-machine"]

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

&lt;/div&gt;



&lt;p&gt;Turns out building the binary on the mac doesn’t work, even using &lt;a href="https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63" rel="noopener noreferrer"&gt;GOOS and GOARCH&lt;/a&gt; didn’t do the trick, so I now build the binary in the container, using the &lt;a href="https://hub.docker.com/_/golang/" rel="noopener noreferrer"&gt;golang&lt;/a&gt; image from Docker hub:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM golang:1.9.3-alpine3.7
ADD . /app
WORKDIR /app
RUN cd /app &amp;amp;&amp;amp; \
    apk update &amp;amp;&amp;amp; \
    apk add git bash &amp;amp;&amp;amp; \
    go get github.com/domnikl/ifttt-webhook &amp;amp;&amp;amp; \
    go get github.com/domnikl/fritz-box &amp;amp;&amp;amp; \
    go build -o washing-machine
CMD ["/app/washing-machine"]

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

&lt;/div&gt;



&lt;p&gt;Hope this will help you in getting around this annoying error message.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
