<?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: Marc Reichelt</title>
    <description>The latest articles on DEV Community by Marc Reichelt (@mreichelt).</description>
    <link>https://dev.to/mreichelt</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%2F217303%2F2af1871d-f2a9-4376-8c62-3463b0cb4256.jpeg</url>
      <title>DEV Community: Marc Reichelt</title>
      <link>https://dev.to/mreichelt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mreichelt"/>
    <language>en</language>
    <item>
      <title>It's Time to Act</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Thu, 23 Jan 2025 12:06:04 +0000</pubDate>
      <link>https://dev.to/mreichelt/its-time-to-act-538</link>
      <guid>https://dev.to/mreichelt/its-time-to-act-538</guid>
      <description>&lt;p&gt;So here we are, living our lives. Just trying to get by. Trying to find our bit of happiness in the world. And then the world implodes.&lt;/p&gt;

&lt;p&gt;All I wanted is to live in peace. To be around people I care about. Mostly mind my own business. To find meaning in my work, and to make my small contribution to make the world better. But I realized there are people working on burning the world to the ground, quite literally: dictators, fascists, greedy oligarchs and corporations are on track to destroy the world and everything we love, just to secure even more trillions of dollars in profits for themselves. How can we possibly cope with that?&lt;/p&gt;

&lt;p&gt;The oil and gas industry worldwide made 2.8 billion dollars in pure profit - every &lt;strong&gt;single day&lt;/strong&gt; over the last 50 years (&lt;a href="https://www.theguardian.com/environment/2022/jul/21/revealed-oil-sectors-staggering-profits-last-50-years" rel="noopener noreferrer"&gt;source&lt;/a&gt;). We're facing an industry that can afford to buy elections, politicians, news outlets, TV channels, social media, create and fund pro-oil NGOs, and much more - and most of that with the profits of a single day. To add to that, it's in their interest to fund fascists, in order to destabilize governments. More power for very few people with money, less power for everyone else.&lt;/p&gt;

&lt;p&gt;The evil forces are shockingly resourceful, organized, and loud. That means we have to be even more resourceful, organized, and even louder. Resourceful in numbers. Organized against evil. And our message has to be heard loud and clear from the rooftops: empathy and love!&lt;/p&gt;

&lt;p&gt;Build your home-base: Talk with friends! Donate to causes you find worthy, like fact-checking news outlets and activists. Speak out if you see intolerance. Attend pro-democracy demonstrations. Boycott companies that spread fake news or extreme statements, and the ones that support extremists: Boycott Tesla, Meta, and fake-news spreading media. And if you haven't, close or at least abandon your accounts on Twitter/X, Facebook, Instagram! Join &lt;a href="https://mastodon.social/" rel="noopener noreferrer"&gt;Mastodon&lt;/a&gt; and &lt;a href="https://bsky.app/" rel="noopener noreferrer"&gt;Bluesky&lt;/a&gt;, and rediscover the joy of having a like-minded and tolerant community! Spread the word on the positive news, because there are many!&lt;/p&gt;

&lt;p&gt;I'd like to end this post on a personal note. To this day, my favourite band is the German punk-rock band &lt;a href="https://www.dth.de/" rel="noopener noreferrer"&gt;Die Toten Hosen&lt;/a&gt;. I always liked their songs, and their lyrics connected with me in a special way due to personal experiences.&lt;br&gt;
It just so happened that many years ago, while attending a live concert in Frankfurt, the front singer Campino threw an open beer can into the audience - and I was the lucky one to catch it, among many others who wanted to catch the can thrown by their idol. But the can landed exactly with the open hole around my middle finger, so I got a pretty scar. I've always laughed it off as a funny thing: "Hey, see - now I'll always remember my favourite band!"&lt;/p&gt;

&lt;p&gt;Today, I decided it's time to lift that finger again - to send one message:&lt;br&gt;
&lt;strong&gt;Fuck intolerance&lt;/strong&gt;!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffd1apmueg4yviademf4g.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffd1apmueg4yviademf4g.jpg" alt="Picture showing the inside of my right hand, the middle finger raised. A scar can be seen from left to right." width="350" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's time to act. Let's get to work.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;PS: follow me on &lt;a href="https://mastodon.social/@mreichelt" rel="noopener noreferrer"&gt;Mastodon&lt;/a&gt; and &lt;a href="https://bsky.app/profile/mreichelt.bsky.social" rel="noopener noreferrer"&gt;Bluesky&lt;/a&gt; if my post resonates with you!&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Kotlin is so nice – it prevents bugs 🐛 before you add them…</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 19 Jun 2020 15:57:03 +0000</pubDate>
      <link>https://dev.to/mreichelt/kotlin-is-so-nice-it-prevents-bugs-before-you-add-them-107l</link>
      <guid>https://dev.to/mreichelt/kotlin-is-so-nice-it-prevents-bugs-before-you-add-them-107l</guid>
      <description>&lt;p&gt;You all know those awesome colleagues who don't let go - after your team fixes a bug, they go the extra mile and make changes that no team member will fall into that trap again. They prevent future developers from making the same mistakes!&lt;/p&gt;

&lt;p&gt;Well, Kotlin and IntelliJ are doing exactly that - for millions of developers worldwide! These are just a few examples of how Kotlin prevents bugs before you even make them.&lt;/p&gt;

&lt;h2&gt;
  
  
  No more &lt;code&gt;switch/case&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you start with Java (or C or C++), you'll make this mistake a few times before learning how to avoid it. Let's see some Java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printOperatingSystem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OperatingSystem&lt;/span&gt; &lt;span class="n"&gt;operatingSystem&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operatingSystem&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nl"&gt;windows:&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Windows"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nl"&gt;mac:&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mac"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nl"&gt;linux:&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Linux"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you call &lt;code&gt;printOperatingSystem(OperatingSystem.linux)&lt;/code&gt;, this will print &lt;code&gt;Linux&lt;/code&gt;. But if you call it with &lt;code&gt;printOperatingSystem(OperatingSystem.windows)&lt;/code&gt;, this will actually print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Windows
Mac
Linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's because we need to put a &lt;code&gt;break&lt;/code&gt; at the end of each case to not fall into the next one. For many developers, this might seem like a simple mistake - because once you learn about it, you learn to avoid it, and it goes into your muscle memory.&lt;/p&gt;

&lt;p&gt;But what if the language would not let you do these mistakes in the first place? Let's see how you would write this 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="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;printOperatingSystem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;operatingSystem&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;OperatingSystem&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;operatingSystem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;OperatingSystem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;windows&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Windows"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;OperatingSystem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mac&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mac"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;OperatingSystem&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linux&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Linux"&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;No more &lt;code&gt;break&lt;/code&gt; to forget, and it's easier to read as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Equality instead of instance check
&lt;/h2&gt;

&lt;p&gt;In Java, if you use &lt;code&gt;==&lt;/code&gt;, this will actually check the instance of an object - not the content. What's really weird is that when you try this out with a simple check like in the third line of the next example, this will actually 'work' - because Java will optimize the Strings to exist only once in memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1234567"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;two&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1234567"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one == two: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;two&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// prints true&lt;/span&gt;

&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;three&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;two&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'7'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'8'&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'8'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'7'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// that's still the same&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"two == three: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;two&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;three&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// prints false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modern IDE tools thankfully warn you about this by giving you a message &lt;em&gt;String values are compared using '==', not 'equals()'&lt;/em&gt; and an auto-fix, but it would be nice if the language itself would choose the most-used equality operator as the default. Kotlin does:&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;val&lt;/span&gt; &lt;span class="py"&gt;one&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1234567"&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;two&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1234567"&lt;/span&gt;
&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"one == two: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;one&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;two&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// prints true&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;three&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;two&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'7'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'8'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'8'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'7'&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="s"&gt;"two == three: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;two&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;three&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// true, as we would expect :-)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  NullPointerExceptions
&lt;/h2&gt;

&lt;p&gt;This is old news for Kotlin devs, but still: every time you avoid nullable types in your codebase prevents these bugs from happening. Every time you hit the &lt;code&gt;.&lt;/code&gt; on a variable or a long expression and Kotlin tells you: "Wait a second, actually that could be null!" is another bug being prevented.&lt;/p&gt;

&lt;p&gt;This is a win-win-win: Users love stable applications, developers love if they don't have to go on a multi-hour bug hunt how that &lt;code&gt;null&lt;/code&gt; value actually made it into the database, and product owners love it as well when developers can use their time to implement new features (and not go on multi-hour bug-hunts instead).&lt;/p&gt;

&lt;p&gt;Here's some Java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getCity&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But actually, the user does not have an address, so &lt;code&gt;getAddress()&lt;/code&gt; can return null. If you didn't develop the best practice to use &lt;code&gt;@NonNull&lt;/code&gt; and &lt;code&gt;@Nullable&lt;/code&gt; annotations, these bugs are going to happen. And they do - all the time. Tony Hoare did call the null reference a &lt;a href="https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions" rel="noopener noreferrer"&gt;billion dollar mistake&lt;/a&gt; for a reason.&lt;/p&gt;

&lt;p&gt;Kotlin will save you here:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff78lmvrrvmyicx6pkimc.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff78lmvrrvmyicx6pkimc.png" alt="Kotlin preventing you from introducing a NullPointerException" width="800" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The beauty about these prevented errors is how Kotlin and the IDE do this. They prevent you from making these mistakes &lt;em&gt;while you are typing&lt;/em&gt;. And usually, it feels so nice when they offer you an auto-fix directly at your fingertips.&lt;/p&gt;

&lt;h2&gt;
  
  
  Coroutines
&lt;/h2&gt;

&lt;p&gt;I have to say I made this mistake in Java quite more times than I thought I would. Let's see some Java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// do something cool here&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you see the mistake? Of course, I called &lt;code&gt;run()&lt;/code&gt; instead of calling &lt;code&gt;start()&lt;/code&gt;. So the code will just do the thing you defined in &lt;code&gt;run&lt;/code&gt; - but it will not do that on the thread, like you expected it to. Today, IntelliJ and other IDEs give you a warning here - but wouldn't it be nice if you didn't need to learn the difference in the first place? What if the tooling would prevent you from making this mistake?&lt;/p&gt;

&lt;p&gt;With Kotlin Coroutines, the IDE will write an error directly when you try to call a suspending function from your regular, non-suspending code:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmwqu338cf0qrcs7xdnr2.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmwqu338cf0qrcs7xdnr2.png" alt="IDE showing a compile error when calling a suspend function from non-suspend context" width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;EDIT: I want to highlight a &lt;a href="https://dev.to/androiddeveloperlb/comment/10kmo"&gt;good comment&lt;/a&gt; by &lt;a href="https://dev.to/androiddeveloperlb"&gt;AndroidDeveloperLB&lt;/a&gt;: Of course Coroutines are something very different than Java Threads. He mentioned that there is a nice &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/thread.html" rel="noopener noreferrer"&gt;thread()&lt;/a&gt; utility method in Kotlin's stdlib. With that, you can write the code above correctly like this (and automatically starting the thread 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="nf"&gt;thread&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something cool here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think this is another great example how Kotlin makes existing APIs easier to handle - with less code and less possible bugs! ❤️&lt;/p&gt;

&lt;h2&gt;
  
  
  Code you don't have to write/generate is code without bugs (usually)
&lt;/h2&gt;

&lt;p&gt;When we write code, we will make mistakes. That's even true for Kotlin code. 😉&lt;/p&gt;

&lt;p&gt;But it's nice if we don't have to wrote some code (mostly boilerplate) in the first place. More often than you'd like, you'll find projects with code like the following (even if tools like Lombok exist, some projects don't use them).&lt;br&gt;
Of course, this is a really bad legacy project, so let's say there are no unit tests either.&lt;/p&gt;

&lt;p&gt;I added an error in the Java code – let's see if you can spot it!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Address&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;PhoneNumber&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Address&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PhoneNumber&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;phoneNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Address&lt;/span&gt; &lt;span class="nf"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;PhoneNumber&lt;/span&gt; &lt;span class="nf"&gt;getPhoneNumber&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
                &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
                &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"User{"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;"firstName='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;", lastName='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;", address="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;", phoneNumber="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="sc"&gt;'}'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you find it? There, in the &lt;code&gt;equals&lt;/code&gt; method, the first line should actually return &lt;code&gt;true&lt;/code&gt; in the instance check. Because right now, it will wrongfully return false for the same instance. Wait, you found a different error? That's because I actually tricked you a little. There are actually two errors here! There, in the &lt;code&gt;getLastName&lt;/code&gt; method, I wrote &lt;code&gt;return firstName&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Oh wait, there are more: I exchanged &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; in the constructor as well. That's not an error in itself, but I'll guarantee you that some colleagues will have a small look at the class, see the members, and instantly conclude that &lt;code&gt;firstName&lt;/code&gt; is the first parameter of the constructor. And they'll wish me to the ground of the deepest sea when they find out what I've done. 😇&lt;/p&gt;

&lt;p&gt;Oh, and in the &lt;code&gt;toString()&lt;/code&gt; method, this will actually print the last name as the first and vice-versa. Good luck finding that bug.&lt;/p&gt;

&lt;p&gt;And did I mention that I removed the &lt;code&gt;lastName&lt;/code&gt; from the &lt;code&gt;equals&lt;/code&gt; method because that felt like a good idea to fix an entirely unrelated function, that now depends on this mistake to be there? These things happen all the time. At least I kept the &lt;code&gt;hashCode()&lt;/code&gt; function correctly. But with a developer like me, you can never know.&lt;/p&gt;

&lt;p&gt;All in all, there are 5 errors in this code! And that with only 4 members. And in big projects, you will only find out about these bugs later on when some user reports a weird behavior. And then the bug-hunt begins.&lt;/p&gt;

&lt;p&gt;With 20 or more members, it becomes extremely common to forget to re-generate this code, or manually add the appropriate lines in the specific functions. Sometimes, some of the necessary changes get lost due to someone not resolving a Git merge conflict properly. 😱&lt;/p&gt;

&lt;p&gt;Long story short: not having to write this code in the first place is a blessing! 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;data class&lt;/span&gt; &lt;span class="nc"&gt;User&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;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Address&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;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PhoneNumber&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There. Constructor, &lt;code&gt;equals&lt;/code&gt;, &lt;code&gt;hashCode&lt;/code&gt;, &lt;code&gt;toString&lt;/code&gt;, getters, setters, &lt;code&gt;copy&lt;/code&gt; function, &lt;code&gt;@NonNull&lt;/code&gt; annotations - all done for you under the hood. Not giving someone like me the opportunity to mess this up. 😅&lt;/p&gt;

&lt;h2&gt;
  
  
  Sealed classes with &lt;code&gt;when&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In the Java &lt;code&gt;switch/case&lt;/code&gt; and &lt;code&gt;if/else&lt;/code&gt; world, it's easy to forget to handle a particular case. Let's say we have this Java code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;CreateUserResult&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;credentials&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"User "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;username&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" created"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;OtherError&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="nc"&gt;OtherError&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Other error occurred: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a user, prints it out on success, and prints the error if it couldn't be created. The type &lt;code&gt;CreateUserResult&lt;/code&gt; is actually a Kotlin sealed class, and one developer added another case that is not handled yet by the Java code - but the IDE does not show any errors. The type is defined like so:&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;CreateUserResult&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;Success&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;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CreateUserResult&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;OtherError&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;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CreateUserResult&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;UserDoesAlreadyExist&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;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CreateUserResult&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;So there actually is another type, called &lt;code&gt;UserDoesAlreadyExist&lt;/code&gt;, which handles the special case that the username is taken already. Let's say the Java code above was written with a Kotlin &lt;code&gt;when&lt;/code&gt; statement, the developer who added &lt;code&gt;UserDoesAlreadyExist&lt;/code&gt; would have gotten an error instantly and could have fixed it directly:&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;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;credentials&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;message&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;result&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;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"User ${result.user.username} created"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OtherError&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Other error occurred: ${result.message}"&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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;when&lt;/code&gt; is used as an expression here, and that way Kotlin says it must be exhaustive. So this will show up in your IDE:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkzzkzwd936ho93sitp35.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkzzkzwd936ho93sitp35.png" alt="when must be exhaustive" width="800" height="105"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Easy enough, the IDE can add the remaining branches automatically for you and it's easy to add the remaining 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="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;message&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;result&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;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"User ${result.user.username} created"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;OtherError&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Other error occurred: ${result.message}"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;UserDoesAlreadyExist&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Error: user ${result.username} does already exist"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another possible bug, prevented before it could even ship to production!&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;These are only a few examples how Kotlin prevents bugs from happening. But actually, Kotlin is full of these. If you use Kotlin: what are your favorite examples of Kotlin preventing bugs before you could even write them? I'd be happy to read your comments!&lt;/p&gt;

&lt;p&gt;If you didn't start using Kotlin yet, I hope you got a small taste of how it could make your developer life better! Because not adding bugs 🐛🐛🐛 in the first place is much better than having to spend days or weeks in hunting them down.&lt;/p&gt;

&lt;p&gt;Have an awesome Kotlin! 🎉&lt;/p&gt;

&lt;p&gt;If you liked this post, please give it a ❤️, click the &lt;code&gt;+FOLLOW&lt;/code&gt; button below and follow me on &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;! This will help me stay motivated to create many more of these posts! 😊&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@louis993546" rel="noopener noreferrer"&gt;Louis Tsai&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/lqcvMiBABHw" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>bugs</category>
      <category>tooling</category>
    </item>
    <item>
      <title>My posts on dev.to: which topics would you like to read about?</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 12 Jun 2020 12:53:40 +0000</pubDate>
      <link>https://dev.to/mreichelt/my-posts-on-dev-to-which-topics-would-you-like-to-read-about-4kko</link>
      <guid>https://dev.to/mreichelt/my-posts-on-dev-to-which-topics-would-you-like-to-read-about-4kko</guid>
      <description>&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1271416542366924801-15" src="https://platform.twitter.com/embed/Tweet.html?id=1271416542366924801"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1271416542366924801-15');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1271416542366924801&amp;amp;theme=dark"
  }



&lt;/p&gt;

</description>
      <category>blogging</category>
      <category>topics</category>
    </item>
    <item>
      <title>Origin story: TechBits by iteratec</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 05 Jun 2020 10:08:46 +0000</pubDate>
      <link>https://dev.to/mreichelt/origin-story-techbits-by-iteratec-3o3i</link>
      <guid>https://dev.to/mreichelt/origin-story-techbits-by-iteratec-3o3i</guid>
      <description>&lt;p&gt;Last november I played around with the idea of creating screencasts. Before, I only &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;tweeted&lt;/a&gt; my thoughts and I &lt;a href="https://medium.com/@mreichelt" rel="noopener noreferrer"&gt;blogged on Medium&lt;/a&gt;. Since I already gave a lot of talks in my life, and I kind of got used to people hearing my voice, creating a screencast would be the next step. I already bought a Blue Yeti microphone years ago for the possibility of creating a podcast or recording music with my guitar - but I didn't take the time to create a podcast or a screencast (yet).&lt;/p&gt;

&lt;p&gt;This is the origin story how '&lt;a href="https://www.youtube.com/playlist?list=PLi_UG4k_p995A0u43XxTuOxERRaiAbA-B" rel="noopener noreferrer"&gt;TechBits by iteratec&lt;/a&gt;' came to be.&lt;/p&gt;

&lt;h2&gt;
  
  
  The possibility
&lt;/h2&gt;

&lt;p&gt;In my company &lt;a href="https://www.iteratec.com/" rel="noopener noreferrer"&gt;iteratec&lt;/a&gt;, all employees can take some days of their work time off and use it for anything they want. This is called &lt;a href="https://karriere.iteratec.de/arbeiten-bei-iteratec/innovationiteratec/#c13870" rel="noopener noreferrer"&gt;Innovation Frei-Day&lt;/a&gt; (page in German). Its a combination of the German word 'frei' (free as in freedom) and 'friday'. It doesn't have to be on a friday - you could also take a whole week - but it's a really nice possibility to do whatever innovative stuff you want, alone or as a hackathon with colleagues, read a book or whatever else you desire.&lt;/p&gt;

&lt;p&gt;I think this is pretty cool, because it's not always possible to try out the things we want when we are constrained by the projects we're on and by the tools we use there. This is the chance to break free and do something that brings you forward. Or plainly to have fun! 😊&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea 💡
&lt;/h2&gt;

&lt;p&gt;Do you know these moments when you talk to a colleague that works on something else, and suddenly in your conversation she comes up with this amazing trick that will cut your build time in half? Or she shows you a keyboard shortcut for your favorite IDE, and from that day on your coding experience is just way more fun? Or you find out about a new tool that you didn't know about, but could totally have used months ago?&lt;/p&gt;

&lt;p&gt;That's what I want to capture in screencasts: Random bits of tricks and tools that made me more productive. Of course some watchers will already know these. But for some, these will be new - and maybe it will spark a light in their minds, and make them be more productive and happier!&lt;/p&gt;

&lt;p&gt;And would be even cooler: to have colleagues contribute ideas and screencasts to those TechBits as well, and publishing them on the iteratec YouTube channel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Brainstorming
&lt;/h2&gt;

&lt;p&gt;In order to find ideas I wanted to create screencasts for, I used a technique called 'journaling'. You take a piece of paper and a pen, and you write down everything that comes to your mind. Everything. Even when you think about nothing, you can write down 'right now I am thinking about nothing'. I first read about this particular form of this technique in the book &lt;a href="https://www.goodreads.com/book/show/12921211-search-inside-yourself" rel="noopener noreferrer"&gt;Search Inside Yourself&lt;/a&gt; by Chade-Meng Tan. It's a book about mindfulness, but I found that this technique helped me a lot in finding nice ideas.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup: Plain and simple
&lt;/h2&gt;

&lt;p&gt;This is how I recorded the screencasts: In my company's office in Frankfurt, with a lovely view outside on the river Main:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcawtsyiv1cqge5y31edj.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcawtsyiv1cqge5y31edj.jpg" alt="Screencast setup" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nothing special. Just my MacBook. The Blue Yeti microphone, connected with a USB cable. A coffee, because I love drinking coffee. And my notes from the aforementioned brain dump.&lt;/p&gt;

&lt;p&gt;On my Mac, I created a new user called 'iterabits' (this was my first draft name for the project) to not show any confidential data or having pop any notifications in on me. I even created a small checklist to check that I was all set for a screencast: Closing the door (with a small hand-written sign that said &lt;strong&gt;Recording in progress&lt;/strong&gt;), turning off my phone and having everything open for the thing I wanted to show.&lt;/p&gt;

&lt;p&gt;Initially, I wanted to record the screencast and do a second voice-over with the microphone later. I quickly dropped that idea, as that would have meant I would need to record twice for each screencast (and time it correctly). Also, I felt that having the tipping sounds and me doing mistakes live would make it more authentic.&lt;/p&gt;

&lt;p&gt;Then, I started QuickTime and recorded the screen. The first time, it took me 3 attempts. It got better for the second and third time. Then I edited them with iMovie, and added a small outro from our company.&lt;/p&gt;

&lt;p&gt;Let's see the results!&lt;/p&gt;

&lt;h2&gt;
  
  
  The first 3 screencasts 🎉
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🍿 TechBit #1: Using Docker as a playground 🎬
&lt;/h3&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/D8M3ge5dwIs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  🍿 TechBit #2: Faster C and C++ builds with ccache
&lt;/h3&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/-EubptaqjHY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  🍿 TechBit #3: IntelliJ - Discover &amp;amp; learn new shortcuts easily 🎬
&lt;/h3&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qjj9psWjxy4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Future
&lt;/h2&gt;

&lt;p&gt;How did you like these? Did you learn something new? Or do you have new tips / ideas what I should do as another screencast in the future? Let me know: I'd love to read your comments, or write me on &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;! Thanks. 🙂&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@hishahadat" rel="noopener noreferrer"&gt;Shahadat Rahman&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/voM1Z9cGPCU" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>screencast</category>
      <category>video</category>
      <category>techtalks</category>
    </item>
    <item>
      <title>The hidden Kotlin gem you didn't think you'll love: Deprecations with ReplaceWith</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 29 May 2020 09:49:04 +0000</pubDate>
      <link>https://dev.to/mreichelt/the-hidden-kotlin-gem-you-didn-t-think-you-ll-love-deprecations-with-replacewith-3blo</link>
      <guid>https://dev.to/mreichelt/the-hidden-kotlin-gem-you-didn-t-think-you-ll-love-deprecations-with-replacewith-3blo</guid>
      <description>&lt;p&gt;This is one of the features in Kotlin that is enabled by IntelliJ IDEA and the Kotlin language working together like a charm, in order to give you a pleasant developer experience. If you look at the &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/replace-with.html" rel="noopener noreferrer"&gt;replaceWith&lt;/a&gt; documentation, you'd probably move on and never give it a second look. Maybe you'll click through to the documentation of the &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecated/" rel="noopener noreferrer"&gt;Deprecated&lt;/a&gt; annotation. At least the &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-replace-with/" rel="noopener noreferrer"&gt;ReplaceWith&lt;/a&gt; docs say what this enables. But because there are no examples, you might just close your browser tab and code on.&lt;/p&gt;

&lt;p&gt;But wait! There is so much to love about this. Trust me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deprecation of subSequence in Kotlin's stdlib
&lt;/h2&gt;

&lt;p&gt;Let's start with an example. In Kotlin 1.3.72, there still is an extension function &lt;code&gt;String.subSequence&lt;/code&gt; that takes a &lt;code&gt;start&lt;/code&gt; and an &lt;code&gt;end&lt;/code&gt; index. But the Kotlin creators wanted it to be more explicit, so they added a new variant that takes &lt;code&gt;startIndex&lt;/code&gt; and &lt;code&gt;endIndex&lt;/code&gt; as parameters instead. So they deprecated the old one with the &lt;code&gt;@Deprecated&lt;/code&gt; annotation - you can see how they did this &lt;a href="https://github.com/JetBrains/kotlin/blob/v1.3.72/libraries/stdlib/src/kotlin/text/Strings.kt#L346-L347" rel="noopener noreferrer"&gt;here&lt;/a&gt;. So if you use the old variant, you'll see it striked through like this:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8stdey9afe838spc8t3k.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8stdey9afe838spc8t3k.png" alt="subSequence is striked through" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But because they added a &lt;code&gt;replaceWith&lt;/code&gt; parameter on that deprecation, IntelliJ will offer you to fix this automatically if you hit &lt;code&gt;Alt + Enter&lt;/code&gt; on it - and you can also decide to automatically fix this &lt;em&gt;in your entire project&lt;/em&gt;!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8owdp9k9bqah2f192pj3.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8owdp9k9bqah2f192pj3.png" alt="IntelliJ offers to automatically replace the old variant" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Live in action it looks like this:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feqo3ke30agfdu3xq2x0d.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Feqo3ke30agfdu3xq2x0d.gif" alt="IntelliJ offers to automatically replace the old variant" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And all the person deprecating the old variant needed to do was to add this one liner before the old variant, worrying only about the code itself, and by doing this enabling millions of developers to be able to change their code in seconds:&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="nd"&gt;@Deprecated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Use parameters named startIndex and endIndex."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ReplaceWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"subSequence(startIndex = start, endIndex = end)"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wouldn't it be nice if you could do the same? Yes, you can!&lt;/p&gt;

&lt;h2&gt;
  
  
  Replace &lt;em&gt;all the things&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Let's use a simple example to start. Say, you have a codebase where someonce decided to put the word &lt;code&gt;do&lt;/code&gt; in front of lots of methods, like &lt;code&gt;doLogout()&lt;/code&gt;. But it's an API, so you can't just get rid of these - you'll have to guide developers to use the new variant &lt;code&gt;logout()&lt;/code&gt; instead. This is how:&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="nd"&gt;@Deprecated&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="s"&gt;"Use simpler logout() instead"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;replaceWith&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ReplaceWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"logout()"&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;doLogout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;logout&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 automatically, &lt;code&gt;doLogout()&lt;/code&gt; will become &lt;del&gt;&lt;code&gt;doLogout()&lt;/code&gt;&lt;/del&gt; for those who use it. And by pressing &lt;code&gt;Alt + Enter&lt;/code&gt; and auto-applying the fix, developers now can update this to &lt;code&gt;logout()&lt;/code&gt; in the entire project.&lt;/p&gt;

&lt;p&gt;Ok, but what about this example? Here, we have the parameters in a peculiar order and a peculiar naming, and we want to be able to clean that up, without breaking the project for anyone:&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;doLogin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pwd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usr&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pwd&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;Well actually, you can use the names &lt;code&gt;pwd&lt;/code&gt; and &lt;code&gt;usr&lt;/code&gt; in your ReplaceWith string, and IntelliJ will automatically insert the expressions used for &lt;code&gt;pwd&lt;/code&gt; and &lt;code&gt;usr&lt;/code&gt; into the replaced 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="nd"&gt;@Deprecated&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="s"&gt;"Use simpler login instead with correct parameter order"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;replaceWith&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ReplaceWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"login(usr, pwd)"&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;doLogin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pwd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usr&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pwd&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;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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;I didn't see the implementation, but if I would take a good guess this has to work by the IDE taking the old Abstract Syntax Tree (AST), building the new AST of the &lt;code&gt;ReplaceWith&lt;/code&gt; string, and copying over the expressions to the right placeholder positions. If anyone finds where this logic is implemented, please send me the GitHub link - I'd love to include it in this blog post! 🌟&lt;/p&gt;

&lt;p&gt;Because it's so nice, here's how this looks if you apply it in the IDE:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzmajya9iw3r890wq4g05.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzmajya9iw3r890wq4g05.gif" alt="doLogin being automatically replaced by IntelliJ IDEA" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  It can do even more: add more code, and imports
&lt;/h2&gt;

&lt;p&gt;Let's say we have the &lt;code&gt;doLogin&lt;/code&gt; method from before, but instead of using two parameters, we want to insert a new &lt;code&gt;Credentials&lt;/code&gt; data class that sits in the &lt;code&gt;auth&lt;/code&gt; package:&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;package&lt;/span&gt; &lt;span class="nn"&gt;auth&lt;/span&gt;

&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Credentials&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;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how we do it:&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="nd"&gt;@Deprecated&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="s"&gt;"Use simpler login with Credentials"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;replaceWith&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ReplaceWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;expression&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"login(Credentials(usr, pwd))"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;imports&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"auth.Credentials"&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;doLogin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pwd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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;When a developer applies the auto-fix with &lt;code&gt;Alt + Enter&lt;/code&gt;, this will automatically add an import as well as fixing the code in-place! This time, let's fix multiple occurrences at the same time, because this looks so cool. Imagine having hundreds of these in your code base, in dozens of files - all fixed automatically! 🎉&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzurib07x8d6m9vyk3tzy.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzurib07x8d6m9vyk3tzy.gif" alt="Replacing multiple occurrences at once, while adding an import" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deprecated can do more
&lt;/h2&gt;

&lt;p&gt;That's it for now about &lt;code&gt;ReplaceWith&lt;/code&gt;! One more thing about &lt;code&gt;@Deprecated&lt;/code&gt; annotation: you can add a &lt;a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deprecation-level/" rel="noopener noreferrer"&gt;DeprecationLevel&lt;/a&gt; to it as well. The default is &lt;code&gt;WARNING&lt;/code&gt;. But if you want to up the game and you want all developers to &lt;em&gt;finally&lt;/em&gt; move over to the new API, you can force them by making this an &lt;code&gt;ERROR&lt;/code&gt;. These have to be fixed first, otherwise their code won't compile. And finally, once all developers have moved over, you can even set this to &lt;code&gt;HIDDEN&lt;/code&gt; - so no one will even see the old method anymore. This is useful if you still want to be binary-compatible. Here is how you can use all these:&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="nd"&gt;@Deprecated&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="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DeprecationLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WARNING&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;warning&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="nd"&gt;@Deprecated&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="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DeprecationLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ERROR&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;error&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="nd"&gt;@Deprecated&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="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;DeprecationLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HIDDEN&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;hidden&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;And here is how they will look when used:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw7n76h66dpj8lj14gn2b.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fw7n76h66dpj8lj14gn2b.png" alt="Effect of all deprecation levels as shown in the IDE" width="490" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this will be a nice addition to your Kotlin toolbox!&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;So to recap, using &lt;code&gt;@Deprecated&lt;/code&gt; with &lt;code&gt;ReplaceWith&lt;/code&gt; lets you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;deprecate old code, and automatically suggest the desired change&lt;/li&gt;
&lt;li&gt;this desired change will be available to all developers working on that code, and they can apply your code change with &lt;code&gt;Alt + Enter&lt;/code&gt; instantly&lt;/li&gt;
&lt;li&gt;it can even re-order the old parameter values if you need them to, or let you add entire new code constructs and imports&lt;/li&gt;
&lt;li&gt;add deprecation levels to decide how fast users of the old code need to fix this&lt;/li&gt;
&lt;li&gt;be more productive and have more fun!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Edit: &lt;a href="https://twitter.com/RussHWolf" rel="noopener noreferrer"&gt;Russell Wolf&lt;/a&gt; added a comment on Twitter that you can use this feature also for discovering APIs, which I think is really cool:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1266384210589372416-137" src="https://platform.twitter.com/embed/Tweet.html?id=1266384210589372416"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1266384210589372416-137');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1266384210589372416&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Have an awesome Kotlin! 🎉&lt;/p&gt;

&lt;p&gt;If you liked this post, please give it a ❤️, click the &lt;code&gt;+FOLLOW&lt;/code&gt; button below and follow me on &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;! This will help me stay motivated to create many more of these posts! 😊&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@mlightbody" rel="noopener noreferrer"&gt;Malcolm Lightbody&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/mfKdQMDSUSU" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>intellij</category>
      <category>tooling</category>
      <category>replacewith</category>
    </item>
    <item>
      <title>What are your Kotlin fail stories? #discuss</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 22 May 2020 10:18:51 +0000</pubDate>
      <link>https://dev.to/mreichelt/what-are-your-kotlin-fail-stories-discuss-ipd</link>
      <guid>https://dev.to/mreichelt/what-are-your-kotlin-fail-stories-discuss-ipd</guid>
      <description>&lt;p&gt;Hello fellow developers! &lt;a href="https://kotlinlang.org/" rel="noopener noreferrer"&gt;Kotlin&lt;/a&gt; has been around for a while, and maybe not every part of our journey was a piece of cake. 🍰&lt;/p&gt;

&lt;p&gt;Was there a project where Kotlin left you in the dark? I want to know about it! What failures (epic or not) did you experience - and how did you move forward? Often, failure stories tell us what things need to be improved. At the very least they make us feel less alone.&lt;/p&gt;

&lt;p&gt;Maybe you tried to introduce Kotlin in a company or a project, but for some reason you were rejected? I totally feel you!&lt;/p&gt;

&lt;p&gt;Please share your &lt;strong&gt;Kotlin fail stories&lt;/strong&gt;! I'll share mine in the comments as well! 👇&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>kotlin</category>
      <category>fail</category>
    </item>
    <item>
      <title>📸 Kotlin Photo Contest - Thank you for your submissions! 🎉</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 15 May 2020 10:01:41 +0000</pubDate>
      <link>https://dev.to/mreichelt/kotlin-photo-contest-thank-you-for-your-submissions-of3</link>
      <guid>https://dev.to/mreichelt/kotlin-photo-contest-thank-you-for-your-submissions-of3</guid>
      <description>&lt;p&gt;Two weeks ago I kicked off the &lt;a href="https://dev.to/mreichelt/the-kotlin-loyalty-free-photo-contest-4ec0"&gt;📸 The Kotlin Royalty-free Photo Contest&lt;/a&gt;. Back then, if you wanted a Kotlin photo for your Kotlin blog post or your new presentation, you were left on your own. Not any more!&lt;br&gt;
I want to give a huge applause to the 3 contributors: &lt;a href="https://twitter.com/kbrhkaya" rel="noopener noreferrer"&gt;Kübra Harmankaya&lt;/a&gt;, &lt;a href="https://twitter.com/louis993546" rel="noopener noreferrer"&gt;Louis Tsai&lt;/a&gt;, and &lt;a href="https://twitter.com/matthewcmckenna" rel="noopener noreferrer"&gt;Matt McKenna&lt;/a&gt;! 👏👏👏&lt;/p&gt;

&lt;p&gt;Without further ado, let's show the photos!&lt;/p&gt;
&lt;h2&gt;
  
  
  Submissions
&lt;/h2&gt;

&lt;p&gt;The first Kotlin photo submitted was by Kübra Harmankaya, showing &lt;a href="https://twitter.com/search?q=%23kotlineverywhere" rel="noopener noreferrer"&gt;#kotlineverywhere&lt;/a&gt; being truly everywhere – this time on the beach:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmi1h7rh4x5eafa4p96m2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmi1h7rh4x5eafa4p96m2.jpg" alt="#kotlineverywhere on the beach" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/_I25PvcsR2U" rel="noopener noreferrer"&gt;Kübra Harmankaya&lt;/a&gt;



&lt;p&gt;The first Kotlin code picture was sent in by Louis Tsai. Awesome!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv1so0cqw3y62f4611uso.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv1so0cqw3y62f4611uso.jpg" alt="Kotlin on a laptop" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/lqcvMiBABHw" rel="noopener noreferrer"&gt;Louis Tsai&lt;/a&gt;



&lt;p&gt;Kübra sent in a photo of a laptop with Kotlin stickers on it. I have to say I love the combination of flowers and tech!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3fsdc46hygx8andanxzb.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3fsdc46hygx8andanxzb.jpg" alt="Kotlin stickers on a laptop" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/92GeLDxLtVE" rel="noopener noreferrer"&gt;Kübra Harmankaya&lt;/a&gt;



&lt;p&gt;Matt McKenna submitted not one, not two, but 7 Kotlin photos! His collection on Unsplash is called &lt;a href="https://unsplash.com/collections/10473670/kotlin-in-the-wild" rel="noopener noreferrer"&gt;Kotlin in the Wild&lt;/a&gt;. I selected 3 photos to show you here. The first one is a photo of colorful Kotlin code:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqi0xsu48a45ds8vdboi2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqi0xsu48a45ds8vdboi2.jpg" alt="Colorful Kotlin code" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/LfjR6IOL7ts" rel="noopener noreferrer"&gt;Matt McKenna&lt;/a&gt;



&lt;p&gt;This one shows Kotlin on a wooden table. I think this not only is a beautiful photo, but it also makes me want to leave my homeoffice, go to the nearby park, set up a WiFi hotspot, and work from there!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvbtuti5rfpyvxik0a1v7.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvbtuti5rfpyvxik0a1v7.jpg" alt="Kotlin on a wooden table" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/izxZ5ozH-a8" rel="noopener noreferrer"&gt;Matt McKenna&lt;/a&gt;



&lt;p&gt;And this one here has a colorful blur of lights in the background, which I think looks great:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff4w6shadr6e3jjvef9d3.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff4w6shadr6e3jjvef9d3.jpg" alt="Kotlin on a laptop, blurred lights behind the laptop" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/Xh12KSq0d1I" rel="noopener noreferrer"&gt;Matt McKenna&lt;/a&gt;



&lt;p&gt;But how could I even select 3 photos out of 7? I think all photos of Matt are great - so I recommend you visit &lt;a href="https://unsplash.com/@mmckenna" rel="noopener noreferrer"&gt;his page on Unsplash&lt;/a&gt; and pick your personal favorite:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg6ryrthu9yihqqxyhhb8.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg6ryrthu9yihqqxyhhb8.png" alt="All #KotlinPhotoContest submissions by Matt McKenna" width="800" height="610"&gt;&lt;/a&gt;&lt;/p&gt;
All submissions by &lt;a href="https://unsplash.com/@mmckenna" rel="noopener noreferrer"&gt;Matt McKenna&lt;/a&gt;



&lt;p&gt;I added two Kotlin photos myself. If you ever need a photo of just the Kotlin logo in the wild, we've got you covered:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa96mdmx7n5197vhc2zsj.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa96mdmx7n5197vhc2zsj.jpg" alt="Kotlin pin in the wild" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;
Photos by &lt;a href="https://unsplash.com/@mreichelt" rel="noopener noreferrer"&gt;Marc Reichelt&lt;/a&gt; (me)


&lt;h2&gt;
  
  
  Thank you! 🎉👏🌟
&lt;/h2&gt;

&lt;p&gt;This goes to you, Kübra, Louis and Matt. Thank you so much for your amazing submissions! All likes ❤️ on this blog post belong to you!&lt;/p&gt;

&lt;p&gt;Dear readers: if you want to see the original photos and future ones, follow them on Unsplash and on Twitter to stay up to date!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kübra Harmankaya on &lt;a href="https://unsplash.com/@kubrahkaya" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; and on &lt;a href="https://twitter.com/kbrhkaya" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Louis Tsai on &lt;a href="https://unsplash.com/@louis993546" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; and on &lt;a href="https://twitter.com/louis993546" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Matt McKenna on &lt;a href="https://unsplash.com/@mmckenna" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; and on &lt;a href="https://twitter.com/matthewcmckenna" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And don't forget to give them credit when you use their photos! 😊&lt;/p&gt;
&lt;h2&gt;
  
  
  A note on Unsplash
&lt;/h2&gt;

&lt;p&gt;If you do a &lt;a href="https://twitter.com/mreichelt/status/1257681035741069312" rel="noopener noreferrer"&gt;search for 'kotlin' on Unsplash&lt;/a&gt;, you will notice that only 2 photos appear - even though nearly all of the photos have 'kotlin' either in the title or in the list of tags - or both. I reached out to Unsplash on Twitter, but haven't received an answer so far:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1257681035741069312-686" src="https://platform.twitter.com/embed/Tweet.html?id=1257681035741069312"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1257681035741069312-686');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1257681035741069312&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Maybe Unsplash has a manual selection process that I was not aware of. Long story short: To see all photos of this contest, you can find them on Twitter by searching for &lt;a href="https://twitter.com/hashtag/KotlinPhotoContest" rel="noopener noreferrer"&gt;#KotlinPhotoContest&lt;/a&gt; – or by bookmarking this blog post. Or by Googling for 'Kotlin Photo Contest'. 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  You have awesome Kotlin photos? Share them!
&lt;/h2&gt;

&lt;p&gt;This contest might be over, but it was there in the first place to kickstart having beautiful Kotlin photos, available for everyone. It served that purpose well. Now, if you have more Kotlin pictures to share yourself, you can use the existing photos as inspiration and add new photos to Unsplash yourself! I'll be happy to like &amp;amp; retweet your photos on Twitter if you add &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;@mreichelt&lt;/a&gt; to your tweet.&lt;/p&gt;

&lt;p&gt;Have an awesome Kotlin! 🎉&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@creativevision" rel="noopener noreferrer"&gt;Nenad Rakicevic&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/IxvC43qJy6U" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>photos</category>
      <category>unsplash</category>
    </item>
    <item>
      <title>A Kotlin developer's view on Flutter/Dart tooling (part 1)</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 08 May 2020 10:37:30 +0000</pubDate>
      <link>https://dev.to/mreichelt/a-kotlin-developer-s-view-on-flutter-dart-tooling-part-1-3j3h</link>
      <guid>https://dev.to/mreichelt/a-kotlin-developer-s-view-on-flutter-dart-tooling-part-1-3j3h</guid>
      <description>&lt;p&gt;&lt;em&gt;Small thing in advance: Comparing Flutter to native development was quite a hot topic (pun intended), and still is. I'm not looking for a 'winner' here. I think there are pros and cons on both sides. Often it is good to look over the fence and see what other languages, tools or ecosystems have to offer. And only by pointing out the flaws and by learning how others do it better we can improve things in our own beloved world. So let's improve!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As you might have guessed from my other blogposts, I'm a huge fan of &lt;a href="https://kotlinlang.org/" rel="noopener noreferrer"&gt;Kotlin&lt;/a&gt;. For me, this began when I first used Kotlin around 3 years ago. Back then, my coworker and friend &lt;a href="https://twitter.com/Niklas_L" rel="noopener noreferrer"&gt;Niklas&lt;/a&gt; and I built a backend in 100% Kotlin. It was one of the most pleasant development experiences I've ever had. Since then, I've built a bunch of apps and other software with it, and was very pleased with the language and the tooling.&lt;/p&gt;

&lt;p&gt;Fast-forward to today, me and an awesome team are developing a &lt;a href="https://flutter.dev/" rel="noopener noreferrer"&gt;Flutter&lt;/a&gt; app for a customer since end of last year. And in Flutter, as you might already know, you'll write almost no Kotlin code (only if you need to have some native functionality). Almost the entire codebase is written in &lt;a href="https://dart.dev/" rel="noopener noreferrer"&gt;Dart&lt;/a&gt;. And having spent some time with Flutter &amp;amp; Dart, I gathered quite a few examples were the tools and the language were lacking and left me in the rain, in a way I didn't expect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Think you really like something? &lt;em&gt;Or: what a deprivation test is&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;To explain to you how I felt I better explain what a deprivation test is. FastCompany has an article on how GitHub &lt;a href="https://www.fastcompany.com/3010972/how-github-uses-deprivation-testing-to-hone-product-design" rel="noopener noreferrer"&gt;uses deprivation testing for product design&lt;/a&gt;. The basic idea is: Someone has a thing. That could be a product, feature or more. Now you give that person a new thing, and let that person use that for some time. Now comes the deprivation part: You take that new thing away from that person. So the person now is back using the old thing. And then you observe their emotions and you collect feedback. Was it ok for them to go back? Or are they missing the new thing - and why?&lt;/p&gt;

&lt;p&gt;I'm sure everyone of you had multiple moments of that in the past. Maybe you had a smartphone that broke, and you had to use an older feature phone for some time, only to be infuriated by it. Or you already used and loved Git, and were forced to use Subversion again because a project didn't migrate yet. Or you had to go back to your old coffee beans because your go-to coffee beans were out of stock, only to find out you hadn't notice how much you liked your go-to coffe beans already.&lt;/p&gt;

&lt;p&gt;To me, I had a very strong deprivation moment when going back to a Java project after developing in Kotlin. I couldn't really tell it before, but it made me love Kotlin &lt;em&gt;even more&lt;/em&gt; - because I discovered that the vast amount of small improvements were making my coding experience so much better than I expected. I couldn't even tell it before what was making me so much happier - but I could tell very easily when those things disappeared.&lt;/p&gt;

&lt;p&gt;And to get back to the topic, I also had a strong deprivation moment when switching from Kotlin to Flutter/Dart. Which I didn't expect, because I actually used Dart (without Flutter) before starting with Kotlin to learn about and implement an &lt;a href="https://github.com/mreichelt/monkey-dart" rel="noopener noreferrer"&gt;interpreter&lt;/a&gt;. Coming from Java I thought Dart was quite nice. But coming from Kotlin, my perspective changed completely. What was I missing? Without further ado: Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Dart, coming from Kotlin
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Missing null safety
&lt;/h3&gt;

&lt;p&gt;The first thing hits hard. For a long time (and still), Dart had no null safety like Kotlin built-in. So you'll find having to write a lot of these (Dart):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;  &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nd"&gt;@required&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@required&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of this (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;Person&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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;age&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And your IDE won't show an error if you write something like &lt;code&gt;Person(name: null, age: null)&lt;/code&gt; in Dart. Brace yourselves for NullPointerExceptions.&lt;/p&gt;

&lt;p&gt;I can't wait for the Dart team to finalize the support for null safety (they are &lt;a href="https://github.com/dart-lang/language/issues/110" rel="noopener noreferrer"&gt;working on it since 1.5 years&lt;/a&gt;), and hope the Dart/Flutter ecosystem - including all the packages and plugins - catches up quickly. If someone of the Dart teams reads this: I think you are doing an amazing job - take your time to finish this feature! 👏 I really hope we'll be able to use it soon.&lt;/p&gt;

&lt;h3&gt;
  
  
  No data classes / sealed classes
&lt;/h3&gt;

&lt;p&gt;Yep, no data classes in Dart built-in. The class above was actually written like this (Dart):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nd"&gt;@required&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@required&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// toString, equals, hashCode not shown for brevity&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In Kotlin, the example above was only missing the &lt;code&gt;data&lt;/code&gt; keyword:&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;data class&lt;/span&gt; &lt;span class="nc"&gt;Person&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&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;age&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are tricks to get something like this in Dart by using generators, but come on - it's 2020, and even &lt;a href="https://aboullaite.me/java-14-records/" rel="noopener noreferrer"&gt;Java 14 has records now&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I want to highlight a talk by &lt;a href="https://twitter.com/passsy" rel="noopener noreferrer"&gt;Pascal Welsch&lt;/a&gt; named &lt;a href="https://www.youtube.com/watch?v=AW3c3-LwT3Q" rel="noopener noreferrer"&gt;Making Dart more like Kotlin&lt;/a&gt;, in which he highlights a pretty new package &lt;a href="https://pub.dev/packages/freezed" rel="noopener noreferrer"&gt;freezed&lt;/a&gt; by Remi Rousselet. This generates code so you can get something like data classes - and even sealed classes - in Dart. It looks promising, but I didn't get my hands on it yet. Still, I believe these features must come to the Dart language as a standard.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enums
&lt;/h3&gt;

&lt;p&gt;Enums in Dart can't have any fields. So this is basically what you can do with enums:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;Environment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;prod&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Want to add a backend URL to all those? Write a switch/case. Want to add a lambda that initializes something for them? Write a switch/case. In Kotlin (as in Java) you can just add them as constructor parameters:&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;enum&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Environment&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;url&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;prod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://prod"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://test"&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;h3&gt;
  
  
  Semicolons;
&lt;/h3&gt;

&lt;p&gt;Semicolons in Dart are everywhere! This is something that I really wouldn't have assumed I would notice, but after spending a lot of time in Kotlin, it actually is noticable, and having no semicolons would make the code more readable.&lt;/p&gt;

&lt;h3&gt;
  
  
  _privateFields
&lt;/h3&gt;

&lt;p&gt;Dart has no &lt;code&gt;private&lt;/code&gt; keyword. Private fields are written with underscore prefix. I really can't stand this, this tiny thing spreads over the whole codebase…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;_message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'please release me'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;why&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;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'is &lt;/span&gt;&lt;span class="si"&gt;${this._message}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_message&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;_message&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;_message&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;' so hard'&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;h3&gt;
  
  
  The new tabs vs. spaces: 'single quotes' vs. "double quotes"
&lt;/h3&gt;

&lt;p&gt;Yep, you can write strings either way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="si"&gt;$foo&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;$bar&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$foo&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;$bar&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's basically the same. And I can't even imagine how many commits there must be out there that change single quotes to double quotes, or the other one around. I mean, developers like to argue about small things anyway. Would be boring otherwise, wouldn't it? 😛&lt;/p&gt;

&lt;h3&gt;
  
  
  if / switch can not be used as expressions
&lt;/h3&gt;

&lt;p&gt;This is a really useful feature in Kotlin, and I didn't think I would miss it. Boy, was I wrong. Let's see a cool example from Kotlin, where we use the combined power of sealed classes, smart casts, and the return value of &lt;code&gt;when&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;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;credentials&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;message&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;result&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;Success&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"User ${result.user.username} created"&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;UserDoesAlreadyExist&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Error: user ${result.username} does already exist"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, &lt;code&gt;when&lt;/code&gt; can be treated as an expression - and the Kotlin compiler will show you an error if you forget to handle a possible branch (less bugs!).&lt;/p&gt;

&lt;h3&gt;
  
  
  Only constant values for optional parameters
&lt;/h3&gt;

&lt;p&gt;I noticed this when I tried to make a lambda parameter non-nullable in Dart, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="kt"&gt;Function&lt;/span&gt; &lt;span class="n"&gt;something&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;This is the error you'll see:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fedvkzbpm2dvcaylhmfu9.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fedvkzbpm2dvcaylhmfu9.png" alt="Default value of an optional parameter must be constant" width="800" height="142"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I tripped a few times over this (not only for lambdas), and it's kind of sad this doesn't work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Constructors
&lt;/h3&gt;

&lt;p&gt;One thing that already hit me a few times in Dart is that &lt;code&gt;final&lt;/code&gt; fields can not be set in the constructor body. Let's see this simple example, where I have two classes &lt;code&gt;ServiceA&lt;/code&gt; and &lt;code&gt;ServiceB&lt;/code&gt;, where the second depends on the first, and I want to initialize them in my class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ServiceA&lt;/span&gt; &lt;span class="n"&gt;serviceA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ServiceB&lt;/span&gt; &lt;span class="n"&gt;serviceB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serviceA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ServiceA&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serviceB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ServiceB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serviceA&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;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy1sgb5nu54wlwl2xl224.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fy1sgb5nu54wlwl2xl224.png" alt="serviceA can't be used as a setter because it's final" width="800" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To do that we would have to implement a series of constructors instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ServiceA&lt;/span&gt; &lt;span class="n"&gt;serviceA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ServiceB&lt;/span&gt; &lt;span class="n"&gt;serviceB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serviceA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serviceB&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serviceA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;serviceB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ServiceB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serviceA&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;MyClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ServiceA&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;As you can see, this grows quite a bit and makes the code unreadable. And we only had &lt;em&gt;one&lt;/em&gt; field that depended on the other. Imagine having 3 or more dependent fields. Yikes.&lt;/p&gt;

&lt;h3&gt;
  
  
  await: Easy to forget
&lt;/h3&gt;

&lt;p&gt;Let's see this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;doSomethingImportant&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// something important here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;doSomethingImportant&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 the default &lt;a href="https://dart.dev/guides/language/analysis-options" rel="noopener noreferrer"&gt;Dart Lint configuration&lt;/a&gt;, it'll just let you do this - and depending on how your code executes, the important stuff will never run. Because you forgot to add an &lt;code&gt;await&lt;/code&gt; keyword in front of the &lt;code&gt;doSomethingImportant()&lt;/code&gt; call. And the IDE didn't warn you. There is a Lint option called &lt;a href="https://dart-lang.github.io/linter/lints/unawaited_futures.html" rel="noopener noreferrer"&gt;unawaited_futures&lt;/a&gt; from the &lt;a href="https://pub.dev/packages/pedantic" rel="noopener noreferrer"&gt;pedantic&lt;/a&gt; package that will prevent you from this. But that option really should be the default.&lt;/p&gt;

&lt;p&gt;Kotlin, with Coroutines, will prevent you from accidentally forgetting 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="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doSomethingImportant&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// something important here&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;doSomethingImportant&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;The IDE shows an error directly, and you must either make that method &lt;code&gt;suspend&lt;/code&gt; as well or wrap it in something like &lt;code&gt;runBlocking&lt;/code&gt;:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl7w2cc7ltrjlxejv4r99.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl7w2cc7ltrjlxejv4r99.png" alt="Kotlin shows error because suspending call is called without Coroutine" width="800" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh, and by the way: You can wrap suspending functions in &lt;code&gt;runBlocking&lt;/code&gt; in Kotlin. You can't in Dart. The only possible option is to slap &lt;code&gt;await&lt;/code&gt; and &lt;code&gt;async&lt;/code&gt; up to the top-most caller, which might be the &lt;code&gt;main&lt;/code&gt; method - forcing you to add those keywords in every function of that stack, sometimes forcing you to entirely re-do your architecture if you missed this. Sometimes, programming languages have to be pragmatic to not stand in the way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collection API is bad
&lt;/h3&gt;

&lt;p&gt;Lists in Dart are mutable by default. If you're used to Kotlin, you'll know how many nice collection functions there are. Most of them are missing in Dart. And in order to have immutable collections, you'll have to write your own. And did I mention that lists don't have equality defined?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kt"&gt;void&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="c1"&gt;// prints false&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;badMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// a has now changed - good luck in finding those bugs introduced by mutable collections&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;badMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// please don't do this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course Dart devs will say that you can use &lt;code&gt;const&lt;/code&gt; instead of &lt;code&gt;final&lt;/code&gt; to get immutable values. And indeed, if we would write &lt;code&gt;const a&lt;/code&gt; we would get an exception when we call &lt;code&gt;a.add(4)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught Error: Unsupported operation: add
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the point of Kotlin's immutable collections is that you didn't even have an &lt;code&gt;add&lt;/code&gt; method to call in the first place.&lt;/p&gt;

&lt;p&gt;Again, there are nice libraries out there that deal with this. But this is something that belongs to the language itself - otherwise projects will never settle on a common language.&lt;/p&gt;

&lt;p&gt;Here is an example how a nice method like &lt;code&gt;associateBy&lt;/code&gt; (which does exist in the &lt;code&gt;dartx&lt;/code&gt; package, but does not exist in Dart) could help every Dart developer to easily create a map from a list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;associateById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;people&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="n"&gt;people&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;associateBy&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&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;If you use Kotlin, you'll get used to use these functions all the time.&lt;/p&gt;

&lt;h3&gt;
  
  
  So many missing stdlib functions
&lt;/h3&gt;

&lt;p&gt;If you come from Kotlin, there are &lt;em&gt;so many&lt;/em&gt; extension functions that make your life easier. Just look at all those extension functions &lt;a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/text/Strings.kt" rel="noopener noreferrer"&gt;defined in String.kt&lt;/a&gt;. There are &lt;code&gt;removePrefix&lt;/code&gt;, &lt;code&gt;removeSurrounding&lt;/code&gt;, &lt;code&gt;substringBefore&lt;/code&gt;, just to name a few. All those together are a massive improvement in developer productivity.&lt;/p&gt;

&lt;p&gt;Again, Dart developers noticed this - and finally that we have extension functions in Dart, we can use those to make code much better. Some Dart developers built the great &lt;a href="https://github.com/leisim/dartx" rel="noopener noreferrer"&gt;dartx&lt;/a&gt; package, which adds all sort of functions to the standard types. And again, this should be something that &lt;em&gt;every Dart developer&lt;/em&gt; should be able to rely on in every project - without setting anything up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Questionable default implementations
&lt;/h3&gt;

&lt;p&gt;If you ever handled files and directories in Dart, you'll see that &lt;code&gt;Directory&lt;/code&gt; and &lt;code&gt;File&lt;/code&gt; have no equality defined. If you ever tried to create a set of these: nope, you'll have to convert them to absolute String paths instead in order for the set to work.&lt;/p&gt;

&lt;p&gt;Another example is the use of &lt;code&gt;dynamic&lt;/code&gt; in APIs. Have a look at the &lt;a href="https://api.flutter.dev/flutter/dart-io/ProcessResult/stdout.html" rel="noopener noreferrer"&gt;stdout&lt;/a&gt; of the &lt;a href="https://api.flutter.dev/flutter/dart-io/ProcessResult-class.html" rel="noopener noreferrer"&gt;ProcessResult&lt;/a&gt; class. It's dynamic. According to the documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Standard output from the process. The value used for the stdoutEncoding argument to Process.run determines the type. If null was used this value is of type List&amp;lt;int&amp;gt; otherwise it is of type String.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we have to just cast to &lt;code&gt;String&lt;/code&gt; and hope it works. Type-safety is important: please don't use &lt;code&gt;dynamic&lt;/code&gt; if you can avoid it.&lt;/p&gt;

&lt;h2&gt;
  
  
  More
&lt;/h2&gt;

&lt;p&gt;Wow, that was already a bunch. And I didn't even run out of things I wanted to write about.&lt;/p&gt;

&lt;p&gt;If you are a Flutter or Dart developer, and you stumbled over at least a few of these things: you're not alone! I feel your pain. Maybe you found even more things I didn't mention: Please &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;write me on Twitter&lt;/a&gt;. I want to collect more of these, so we have a good overview what we as a community can improve in the future.&lt;/p&gt;

&lt;p&gt;As I started this blog post I had in mind to write about the shortcomings of Dart as well as Flutter tooling. I realized there is so much I found with Dart - I didn't even get to the Flutter bit yet. That's why there will be a &lt;strong&gt;part 2&lt;/strong&gt; of this post soon! &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;Follow me on Twitter&lt;/a&gt; and click the &lt;code&gt;+FOLLOW&lt;/code&gt; button below to get the new post while it's hot!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@christopher__burns" rel="noopener noreferrer"&gt;Christopher Burns&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/Wiu3w-99tNg" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>kotlin</category>
      <category>dart</category>
      <category>tooling</category>
    </item>
    <item>
      <title>📸 The Kotlin Royalty-free Photo Contest</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 01 May 2020 10:37:14 +0000</pubDate>
      <link>https://dev.to/mreichelt/the-kotlin-loyalty-free-photo-contest-4ec0</link>
      <guid>https://dev.to/mreichelt/the-kotlin-loyalty-free-photo-contest-4ec0</guid>
      <description>&lt;p&gt;&lt;strong&gt;The Kotlin photo contest is finished! See the submissions here:&lt;/strong&gt;&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/mreichelt" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F217303%2F2af1871d-f2a9-4376-8c62-3463b0cb4256.jpeg" alt="mreichelt"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/mreichelt/kotlin-photo-contest-thank-you-for-your-submissions-of3" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;📸 Kotlin Photo Contest - Thank you for your submissions! 🎉&lt;/h2&gt;
      &lt;h3&gt;Marc Reichelt ・ May 15 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#kotlin&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#photos&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#unsplash&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Some time ago I was creating a Kotlin talk. And while drafting the slides I did some research which images I could show. So I did what I prefer to do most: Open &lt;a href="https://unsplash.com" rel="noopener noreferrer"&gt;unsplash.com&lt;/a&gt; and look there. If you don't know it, Unsplash is an amazing site which offers freely usable images for everyone. And the quality of the images is stunning! What's even better is that you can use all the photos for commercial and non-commercial purposes. But this time, I typed the keyword &lt;a href="https://unsplash.com/s/photos/kotlin" rel="noopener noreferrer"&gt;'kotlin'&lt;/a&gt;. At the time I write this blog post, this is what I found: Gaining emptiness!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fijmasjdpg55wjga2f53v.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fijmasjdpg55wjga2f53v.png" alt="Unsplash search for Kotlin shows zero results" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Logically, I thought: Alright, Kotlin is too specific. Let's search for &lt;a href="https://unsplash.com/s/photos/code" rel="noopener noreferrer"&gt;'code'&lt;/a&gt; instead. And indeed, some beautiful images came up. Here some examples.&lt;/p&gt;

&lt;p&gt;This one here is really nice: the lighting, the colors, the scene set just perfectly – but it's showing JavaScript:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffosqncfwvg9y5ijhinds.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffosqncfwvg9y5ijhinds.jpg" alt="Photo showing JavaScript on a MacBook" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/95YRwf6CNw8" rel="noopener noreferrer"&gt;Clément H&lt;/a&gt;



&lt;p&gt;This one here shows only code on screen, and emphasises the syntax highlighting. But who of you would even write (or read) minified code? Usually, we developers prefer nice, tidy code instead.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi4idl2vjv3shguebnyal.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fi4idl2vjv3shguebnyal.jpg" alt="Photo showing minified code" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/MgtHZ4zlC1U" rel="noopener noreferrer"&gt;Markus Spiske&lt;/a&gt;



&lt;p&gt;Here are two developers working together on something. Wouldn't it be nice if we could have photos of developers working on a Kotlin project, or with some Kotlin cups or stickers nearby?&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fexcetpvsm36l6sxr8vup.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fexcetpvsm36l6sxr8vup.jpg" alt="Group of developers working together" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;
Photo by &lt;a href="https://unsplash.com/photos/IgUR1iX0mqM" rel="noopener noreferrer"&gt;NESA by Makers&lt;/a&gt;



&lt;p&gt;I found pictures of &lt;a href="https://unsplash.com/photos/1IW4HQuauSU" rel="noopener noreferrer"&gt;JavaScript&lt;/a&gt;, &lt;a href="https://unsplash.com/photos/ieic5Tq8YMk" rel="noopener noreferrer"&gt;Python&lt;/a&gt;, &lt;a href="https://unsplash.com/photos/_yMciiStJyY" rel="noopener noreferrer"&gt;CSS&lt;/a&gt;, &lt;a href="https://unsplash.com/photos/5Ntkpxqt54Y" rel="noopener noreferrer"&gt;HTML&lt;/a&gt;, &lt;a href="https://unsplash.com/photos/1td5Iq5IvNc" rel="noopener noreferrer"&gt;Java&lt;/a&gt;, &lt;a href="https://unsplash.com/photos/OqtafYT5kTw" rel="noopener noreferrer"&gt;PHP&lt;/a&gt; and even a photo of some &lt;a href="https://unsplash.com/photos/Ek9Znm8lQ1U" rel="noopener noreferrer"&gt;SVG&lt;/a&gt; code. That's nice if you were looking for those. But no sign of Kotlin.&lt;/p&gt;

&lt;p&gt;Wouldn't it be nice to have some photos that show how clean and readable Kotlin can get? Wouldn't it be nice, the next time you create a blog post, a talk or simply a web page, to have a collection of diverse Kotlin photos to pick from? Wouldn't it be nice to show the world your favorite language?&lt;/p&gt;

&lt;h2&gt;
  
  
  🌟 The Contest
&lt;/h2&gt;

&lt;p&gt;I proudly present to you: &lt;strong&gt;The Kotlin loyalty-free Photo Contest&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;Create photos or pick favorites of which you already made, and upload them to &lt;a href="https://unsplash.com/" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;. Remember to add &lt;code&gt;kotlin&lt;/code&gt; to your photo title Tag your photos with &lt;code&gt;kotlin&lt;/code&gt;, as well as all other tags that are fitting. Whether it is Kotlin code, a group of Kotlin developers, or photos with the Kotlin logo: The more creative the photos are, the more fun! 🎨&lt;/p&gt;

&lt;p&gt;The contest runs for 2 weeks and ends on &lt;strong&gt;May 15th&lt;/strong&gt;. Then I will create another post - highlighting the 7 most noteworthy photos!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;🚨🚨🚨Update🚨🚨🚨&lt;/strong&gt;: Your photo might not appear in the &lt;a href="https://unsplash.com/s/photos/kotlin" rel="noopener noreferrer"&gt;search results for 'kotlin'&lt;/a&gt; instantly, even after a few days. I contacted Unsplash about this. Don't worry though: Just post a link to your photo on Twitter with the hashtag &lt;a href="https://twitter.com/search?q=%23KotlinPhotoContest" rel="noopener noreferrer"&gt;#KotlinPhotoContest&lt;/a&gt;, or leave a comment below! I'll make sure no awesome photo gets overlooked! 🙂
&lt;/h3&gt;

&lt;h2&gt;
  
  
  The Fineprint
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Quality over quantity:&lt;/strong&gt; Please don't upload any photo you have, but pick just one or very few personal favorites, and make sure those are of high quality.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rights&lt;/strong&gt;: You must own the rights to the photos you upload.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;License&lt;/strong&gt;: If you upload photos to Unsplash, you agree to their &lt;a href="https://unsplash.com/license" rel="noopener noreferrer"&gt;license&lt;/a&gt; and &lt;a href="https://help.unsplash.com/en/articles/2534415-submission-guidelines" rel="noopener noreferrer"&gt;submission guidelines&lt;/a&gt;. Be aware that if you upload a photo there that anyone will be able to use your photo anywhere for free, without having to give you credit (though many will give you credit anyway).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stay safe!&lt;/strong&gt; We are experiencing a pandemic. Whether you are at work, home or in nature: Always remember to maintain a safe distance, wash your hands and overall: stay safe.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stay in contact
&lt;/h2&gt;

&lt;p&gt;I can't wait to see the photos you come up with! If you share your Unsplash photo on Twitter, use the hashtag &lt;a href="https://twitter.com/search?q=%23KotlinPhotoContest" rel="noopener noreferrer"&gt;#KotlinPhotoContest&lt;/a&gt; to share and communicate with others. Also, @ me with &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;@mreichelt&lt;/a&gt;, and I'll promise to not be shy with likes and retweets! 😊&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If your reading this post and you have a different language that is close to your heart - feel free to give your language some love! For example, I didn't see photos for Swift, Rust, Dart (with Flutter) and many more…&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>photos</category>
      <category>unsplash</category>
    </item>
    <item>
      <title>Make your code beautiful with Kotlin Property Delegates</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Fri, 24 Apr 2020 10:05:47 +0000</pubDate>
      <link>https://dev.to/mreichelt/make-your-code-beautiful-with-kotlin-property-degelates-1ld5</link>
      <guid>https://dev.to/mreichelt/make-your-code-beautiful-with-kotlin-property-degelates-1ld5</guid>
      <description>&lt;p&gt;My colleague &lt;a href="https://twitter.com/icyerasor" rel="noopener noreferrer"&gt;Andreas Feldschmid&lt;/a&gt; and I are about to prepare advanced Kotlin topics for the second day of a two-day Kotlin training. It's pretty amazing that we are now Kotlin trainers at our company &lt;a href="https://iteratec.com" rel="noopener noreferrer"&gt;iteratec&lt;/a&gt; - and I'll write another blogpost about how that came to be in the upcoming weeks. By the way: if there is anything that &lt;strong&gt;you&lt;/strong&gt; would love to learn in an advanced Kotlin training: please leave a comment below or write me &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;on Twitter&lt;/a&gt;! 📩&lt;/p&gt;

&lt;p&gt;In order to select topics for this training, we were talking about lots of Kotlin features and libraries. And one topic that came up were &lt;strong&gt;Delegated Properties&lt;/strong&gt;. He heard about them, but didn't use them yet. So I quickly opened IntelliJ and showed him this example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small recap on properties
&lt;/h2&gt;

&lt;p&gt;This is pretty standard: In Kotlin, we can define mutable properties with &lt;code&gt;var&lt;/code&gt; and values with &lt;code&gt;val&lt;/code&gt;. Let's see some 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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Preferences&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;username&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have a property &lt;code&gt;username&lt;/code&gt;. And each mutable property has a getter and a setter, which you can override. By the way, if you don't know the syntax by memory, you can simply type &lt;code&gt;Alt + Enter&lt;/code&gt; on &lt;code&gt;username&lt;/code&gt; and get the quick action for that:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdalk955w0e3kklztdcwi.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdalk955w0e3kklztdcwi.png" alt="Type Alt + Enter on a property in IntelliJ to add getter and setter methods" width="800" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's more to read about that in the &lt;a href="https://kotlinlang.org/docs/reference/properties.html" rel="noopener noreferrer"&gt;Kotlin docs on properties&lt;/a&gt;. But instead of implementing properties yourself, you can instead create &lt;em&gt;delegated properties&lt;/em&gt;, where the getter and setter are provided someplace else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Awesome code with delegated properties
&lt;/h2&gt;

&lt;p&gt;A delegated property, as &lt;a href="https://kotlinlang.org/docs/reference/delegated-properties.html" rel="noopener noreferrer"&gt;described in the Kotlin docs&lt;/a&gt; is simply defined by using the &lt;code&gt;by&lt;/code&gt; keyword and a delegate instance:&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;Preferences&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;username&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;Delegate&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;This can be any class that implements the operator functions &lt;code&gt;getValue&lt;/code&gt; and &lt;code&gt;setValue&lt;/code&gt;. You can copy those from the documentation - but you can simply let IntelliJ create that class for you. And there are interfaces &lt;code&gt;ReadWriteProperty&lt;/code&gt; and &lt;code&gt;ReadOnlyProperty&lt;/code&gt; available which can help you with the syntax as well.&lt;/p&gt;

&lt;p&gt;As an example, we'll create a &lt;code&gt;FileDelegate&lt;/code&gt; that reads and writes text to a file, and takes a file name as parameter:&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;Preferences&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;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"username.txt"&lt;/span&gt;&lt;span class="p"&gt;)&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;FileDelegate&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;fileName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ReadWriteProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Preferences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;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;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Preferences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&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;file&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readText&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&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;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Preferences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;writeText&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="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 we can access the content of the file &lt;code&gt;username.txt&lt;/code&gt; by accessing the property! Isn't that cool?&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="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;preferences&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Preferences&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;preferences&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Marc"&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;preferences&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&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;Even better, we now can reuse the same functionality with multiple properties:&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;Preferences&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;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"username.txt"&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;accessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"accessToken.txt"&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;favoriteFood&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"favoriteFood.txt"&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;Wouldn't it be nice if we could even generate the filename according to the name of the property? Easy as pie, because each &lt;code&gt;KProperty&lt;/code&gt; has a &lt;code&gt;property.name&lt;/code&gt; we can use:&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;FileDelegate&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ReadWriteProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Preferences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;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;getValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Preferences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&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;file&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;".txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readText&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&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;setValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisRef&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Preferences&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KProperty&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;*&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;".txt"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;writeText&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="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;While only a small change, this makes the code even more readable. Of course, this comes at a cost we should be aware of: If someone refactors a property name, the filename will change, too. If we are willing to accept this, the code will read 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;Preferences&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;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&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;accessToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&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;favoriteFood&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&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 in addition, because we extend from &lt;code&gt;ReadWriteProperty&amp;lt;Preferences, String&amp;gt;&lt;/code&gt;, we can let type inference automatically get the &lt;code&gt;String&lt;/code&gt; return type:&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;Preferences&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;username&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&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;accessToken&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&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;favoriteFood&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&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;Let's say that &lt;code&gt;favoriteFood.txt&lt;/code&gt; is generated by a different program, and we only want to read it here. We can just change &lt;code&gt;var&lt;/code&gt; to &lt;code&gt;val&lt;/code&gt;, which effectively hides the setter:&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;val&lt;/span&gt; &lt;span class="py"&gt;favoriteFood&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nc"&gt;FileDelegate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think this can come in pretty handy, and delegated properties can make your Kotlin projects nicer to read. Some things you could use delegated properties for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading/writing things from disk&lt;/li&gt;
&lt;li&gt;Caching values as well (instead of reading them again every time)&lt;/li&gt;
&lt;li&gt;Reading/writing shared preferences in Android (see &lt;a href="https://medium.com/@krzychukosobudzki/sharedpreferences-and-delegated-properties-in-kotlin-5437feeb254d" rel="noopener noreferrer"&gt;this great blog post&lt;/a&gt; on how to do that)&lt;/li&gt;
&lt;li&gt;Getting something from the network (although you'll have to call &lt;code&gt;runBlocking&lt;/code&gt; if you use coroutines, as this mechanism currently does not support suspending coroutines - at least not that I know of)&lt;/li&gt;
&lt;li&gt;Do logging&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Wait, there's more!
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://kotlinlang.org/docs/reference/delegated-properties.html" rel="noopener noreferrer"&gt;Kotlin documentation on delegated properties&lt;/a&gt; contains more knowledge. For example, I already knew about the &lt;code&gt;lazy&lt;/code&gt; delegate from Kotlin's stdlib, which can be used to get and store the result of some expensive operation:&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;val&lt;/span&gt; &lt;span class="py"&gt;lazyValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;calculateAnswer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// takes 7.5 million years to print 42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What I didn't know was that you can define the thread safety mode (as lazy is synchronized by default, which can add more overhead than you would expect):&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;val&lt;/span&gt; &lt;span class="py"&gt;lazyValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LazyThreadSafetyMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NONE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;calculateAnswer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// takes 7.5 million years to print 42&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another useful thing I didn't know yet was that you can delegate to a map, like so for an immutable map as stated by the Kotlin docs:&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;User&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;map&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Any&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;     &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Have an awesome Kotlin! 🎉&lt;/p&gt;

&lt;p&gt;If you liked this post, please give it a ❤️, click the &lt;code&gt;+FOLLOW&lt;/code&gt; button below and follow me on &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;! This will help me stay motivated to create many more of these posts! 😊&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@antonsulsky" rel="noopener noreferrer"&gt;Anton Sulsky&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/YcfCXxo7rpc" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>kotlin</category>
    </item>
    <item>
      <title>Handling SIGINT in Kotlin Native</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Wed, 15 Jan 2020 09:11:03 +0000</pubDate>
      <link>https://dev.to/mreichelt/handling-sigint-in-kotlin-native-2ac6</link>
      <guid>https://dev.to/mreichelt/handling-sigint-in-kotlin-native-2ac6</guid>
      <description>&lt;p&gt;Just 2 weeks ago Patrick Lemke on the Kotlin Slack in the #kotlin-native channel asked an &lt;a href="https://kotlinlang.slack.com/archives/C3SGXARS6/p1577698245105700" rel="noopener noreferrer"&gt;interesting question&lt;/a&gt;:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gl1mllfke5oahh173pg.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gl1mllfke5oahh173pg.png" alt="Original question by Patrick Lemke: How do I react to a SIGINT event in Kotlin Native?" width="800" height="96"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was intrigued by this question. A SIGINT is the event sent to a process if you press &lt;code&gt;Ctrl + C&lt;/code&gt; in the shell, and that usually exits a process. So how would you actually handle events like SIGINT in Kotlin Native? 🤔&lt;/p&gt;

&lt;p&gt;At first I thought this would be hard to implement. I tried to create a small example. On my way I not only managed to implement this, but discovered how Kotlin tooling and documentation can help you in solving similar problems. Onward!&lt;/p&gt;

&lt;h2&gt;
  
  
  How C/C++ handle SIGINT
&lt;/h2&gt;

&lt;p&gt;At first it's probably a good idea to read about how these interrupts are actually created in C/C++. I searched for 'handle sigint c/c++' on Google and found a page on &lt;a href="https://www.tutorialspoint.com/cplusplus/cpp_signal_handling.htm" rel="noopener noreferrer"&gt;C++ Signal Handling&lt;/a&gt;. Basically, you define a function in C++ and you can register it as a signal handler by calling the &lt;code&gt;signal&lt;/code&gt; function (that's the only C++ code I'll show here):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;signalHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;signum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// your logic here when interrupt happens&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&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="c1"&gt;// register your handler function - can be SIGINT, SIGABRT, or others&lt;/span&gt;
    &lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;signalHandler&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// ever-running code here that can/will be interrupted&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Calling &lt;code&gt;signal&lt;/code&gt; in Kotlin Native
&lt;/h2&gt;

&lt;p&gt;Now let's find out how we can call that function in Kotlin Native! First, I opened IntelliJ, and created a new Kotlin Native project with &lt;code&gt;Create New Project -&amp;gt; Kotlin -&amp;gt; Native | Gradle&lt;/code&gt;. This automatically creates a sample main file. Let's see if it can find a &lt;code&gt;signal&lt;/code&gt; function. And jackpot - there it is, and it can be imported!&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ud7xxj73lkk4uucqj2m.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ud7xxj73lkk4uucqj2m.png" alt="signal function from platform.posix in IntelliJ" width="800" height="113"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And SIGINT and the other constants can be found as well:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4cp3bk0i62p5fzpu3yb.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft4cp3bk0i62p5fzpu3yb.png" alt="SIGINT, SIGTERM, and more" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I think this is tooling by IntelliJ/Kotlin is pretty amazing. It looks simple, but there is actually a whole lot going on under the covers. The POSIX bindings are created automatically for you and the platform(s) you target, and they are already indexed, waiting for you to type-and-complete.&lt;/p&gt;

&lt;p&gt;Now, moving to the second parameter: we already know this is a lambda. But what the heck is a &lt;code&gt;CPointer&amp;lt;CFunction&amp;lt;(Int) → Unit&amp;gt;&amp;gt;?&lt;/code&gt; - and how do I get one? I had no idea, because I've never done this before in Kotlin (although I know what a pointer is). So I googled for 'kotlin cpointer cfunction', and landed on this excellent Kotlin documentation: &lt;a href="https://kotlinlang.org/docs/tutorials/native/mapping-function-pointers-from-c.htm" rel="noopener noreferrer"&gt;Mapping Function Pointers from C&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And there it is - scrolling down I find exactly what I need: &lt;a href="https://kotlinlang.org/docs/tutorials/native/mapping-function-pointers-from-c.html#passing-kotlin-function-as-c-function-pointer" rel="noopener noreferrer"&gt;Passing Kotlin Function as C Function Pointer&lt;/a&gt; Using that, we now know that Kotlin provides a helper function called &lt;code&gt;staticCFunction&lt;/code&gt;. Let's use that!&lt;/p&gt;

&lt;p&gt;So a trivial example might be that we print something forever. And then we will interrupt it. Here's my first, trivial implementation:&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="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;staticCFunction&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;,&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="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Interrupt: $it"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&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;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"running"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;sleep&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we compile this (by clicking the &lt;code&gt;Build&lt;/code&gt; button), it will create a binary with the name of our project and a &lt;code&gt;.kexe&lt;/code&gt; ending. For me, it created a file under &lt;code&gt;build/bin/macos/debugExecutable/kotlin-native-signal.kexe&lt;/code&gt;. I wanted to call the main method directly via the common 'green arrow run button', but that wasn't there. For now, we'll use the commandline - but I hope JetBrains continues to improve the tooling around Kotlin Native (see that section further down).&lt;/p&gt;

&lt;p&gt;So let's call the program in a shell, and press &lt;code&gt;Ctrl + C&lt;/code&gt; after 2 seconds to interrupt it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;build/bin/macos/debugExecutable/kotlin-native-signal.kexe
&lt;span class="go"&gt;running
running
^CInterrupt: 2
running
running
running
[program continues, does not exit]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nice, it does handle the interrupt! 🙂&lt;/p&gt;

&lt;p&gt;But we didn't actually exit the program in our interrupt handler - so now the program will continue forever. Unless we kill it with &lt;code&gt;killall&lt;/code&gt;… 😈&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# in a second shell:
$ killall kotlin-native-signal.kexe

# in the first shell this will kill the program:
[1]    66077 terminated  build/bin/macos/debugExecutable/kotlin-native-signal.kexe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To improve this program, we can call the (POSIX function!) &lt;code&gt;exit&lt;/code&gt; in the interrupt. Also, we can use &lt;code&gt;staticCFunction&lt;/code&gt; to create a CPointer reference to an existing Kotlin function as well. Here's the improved version, with imports added for reference:&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;import&lt;/span&gt; &lt;span class="nn"&gt;kotlinx.cinterop.staticCFunction&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;platform.posix.SIGINT&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;platform.posix.exit&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;platform.posix.signal&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;platform.posix.sleep&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;handleSignal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;signalNumber&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;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Interrupt: $signalNumber"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;staticCFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;handleSignal&lt;/span&gt;&lt;span class="p"&gt;))&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;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"running"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;sleep&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, when we interrupt the program with &lt;code&gt;Ctrl + C&lt;/code&gt;, it will run our handler code and exit.&lt;/p&gt;

&lt;h2&gt;
  
  
  IntelliJ: A tooling improvement suggestion
&lt;/h2&gt;

&lt;p&gt;When I wrote this blogpost I wanted to run the &lt;code&gt;main&lt;/code&gt; method directly from the editor. But where usually I expect to find a green run icon of happiness there was just an empty grey area:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vgkgo0ha1g5y05y7tav.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vgkgo0ha1g5y05y7tav.png" alt="Missing run icon in IntelliJ Community edition" width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first I tried the free community edition of IntelliJ. Next, I tried IntelliJ Ultimate. I opened the same project. Ultimate suggested I install a plugin, so I did that:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5he2yo9f50ts92016in.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5he2yo9f50ts92016in.png" alt="IntelliJ Ultimate: Plugin for Kotlin Native" width="738" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nevertheless: even after doing that and restarting the IDE, Ultimate didn't show me the green run button. In the 'Run Configurations', I got a new entry 'Kotlin/Native Application' - but it didn't detect my Kotlin Native executable, so I couldn't select that.&lt;/p&gt;

&lt;p&gt;Finally, I tried CLion. I installed an additional plugin for Kotlin Native. Still, no run icon to the left of the &lt;code&gt;main&lt;/code&gt; method. But at least I got a run configuration that way, and I could run &amp;amp; debug the Kotlin Native program from the IDE! 🎉&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn36qbg6tdxtpb3cgi1cd.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn36qbg6tdxtpb3cgi1cd.png" alt="CLion: debug a Kotlin Native program" width="668" height="50"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So for tooling, I have the following wishes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I'd love to have that green run icon. It's there in C/C++ programs in CLion and in all Java projects, so why not here?&lt;/li&gt;
&lt;li&gt;IntelliJ Community could make the suggestion that run/debug is only supported in the commercial tools.&lt;/li&gt;
&lt;li&gt;IntelliJ Ultimate and CLion should behave consistently. What works in CLion should work in Ultimate as well.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;This was a fun ride! It's so amazing that we can use POSIX APIs and more (iOS &amp;amp; MacOS libs, etc.) in Kotlin Native - and the coding tooling is great already. There are still some things that could be improved. Then again, Kotlin Native is still a relatively new technology. But it's already on a good way to becoming stable!&lt;/p&gt;

&lt;p&gt;If you liked this post, please give it a ❤️ and follow me on &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>c</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Christmas Hacking: Squeezing Kotlin Native into Docker</title>
      <dc:creator>Marc Reichelt</dc:creator>
      <pubDate>Wed, 25 Dec 2019 22:49:39 +0000</pubDate>
      <link>https://dev.to/mreichelt/christmas-hacking-squeezing-kotlin-native-into-docker-6ao</link>
      <guid>https://dev.to/mreichelt/christmas-hacking-squeezing-kotlin-native-into-docker-6ao</guid>
      <description>&lt;p&gt;Some time ago, I had a conversation with my fellow GDG organizer &lt;a href="https://twitter.com/randygupta" rel="noopener noreferrer"&gt;Randy Gupta&lt;/a&gt;. He's a Google Developer Expert on cloud, and he blew my mind when he said that you can create Docker images containing a single, self-contained executable. Think about it: no libraries, no package manager - just a single program doing it's job. That's amazing!&lt;/p&gt;

&lt;p&gt;So because I love Kotlin, I wondered how far we can take this using &lt;a href="https://kotlinlang.org/docs/reference/native-overview.html" rel="noopener noreferrer"&gt;Kotlin Native&lt;/a&gt;. With that, we can compile Kotlin code as an executable. Then we can add this to a Docker image. Let's begin, and have some fun! 😊&lt;/p&gt;

&lt;h2&gt;
  
  
  Kotlin, Go Native
&lt;/h2&gt;

&lt;p&gt;As a first step, we'll create a simple hello world program and compile that to native. There's a good &lt;a href="https://kotlinlang.org/docs/tutorials/native/basic-kotlin-native-app.html" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt; to start. Our &lt;code&gt;hello_world.kt&lt;/code&gt; program:&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="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&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 we can compile and run it natively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;kotlinc-native hello_world.kt &lt;span class="nt"&gt;-o&lt;/span&gt; hello_world
&lt;span class="go"&gt;
&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;./hello_world.kexe
&lt;span class="go"&gt;Hello, World!

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;file hello_world.kexe
&lt;span class="go"&gt;hello_world.kexe: Mach-O 64-bit executable x86_64
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, this works! But as you can see, because I ran this on my Mac, this created a Mac executable. For Docker, we will need a Linux executable. We can pass &lt;code&gt;-target linux&lt;/code&gt; as a parameter to &lt;code&gt;kotlinc-native&lt;/code&gt; to do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;kotlinc-native &lt;span class="nt"&gt;-target&lt;/span&gt; linux hello_world.kt &lt;span class="nt"&gt;-o&lt;/span&gt; hello_world_linux
&lt;span class="go"&gt;
&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;ll &lt;span class="k"&gt;*&lt;/span&gt;.kexe
&lt;span class="go"&gt;-rwxr-xr-x  1 mreichelt  mreichelt   607K 25 Dez 19:06 hello_world.kexe
-rwxr-xr-x  1 mreichelt  mreichelt   375K 25 Dez 19:14 hello_world_linux.kexe

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;file hello_world_linux.kexe
&lt;span class="go"&gt;hello_world_linux.kexe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.16, BuildID[xxHash]=421d2b66bd8de945, not stripped
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, cool - we have a Linux binary! Docker will be so happy… 🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker + Kotlin Native = ❤️
&lt;/h2&gt;

&lt;p&gt;Let's pack this into a Docker image using a &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; ubuntu&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; hello_world_linux.kexe /hello_world&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["/hello_world"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can build the Docker image &amp;amp; run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker build &lt;span class="nt"&gt;--tag&lt;/span&gt; hello_kotlin &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="go"&gt;Sending build context to Docker daemon  5.179MB
Step 1/3 : FROM ubuntu
&lt;/span&gt;&lt;span class="gp"&gt; ---&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;a2a15febcdf3
&lt;span class="go"&gt;Step 2/3 : COPY hello_world_linux.kexe /hello_world
&lt;/span&gt;&lt;span class="gp"&gt; ---&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;9438118fba59
&lt;span class="go"&gt;Step 3/3 : ENTRYPOINT ["/hello_world"]
&lt;/span&gt;&lt;span class="gp"&gt; ---&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;Running &lt;span class="k"&gt;in &lt;/span&gt;2454425124e7
&lt;span class="go"&gt;Removing intermediate container 2454425124e7
&lt;/span&gt;&lt;span class="gp"&gt; ---&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;47b7a9b05993
&lt;span class="go"&gt;Successfully built 47b7a9b05993
Successfully tagged hello_kotlin:latest

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; hello_kotlin
&lt;span class="go"&gt;Hello, World!

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker image list hello_kotlin
&lt;span class="go"&gt;REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello_kotlin        latest              47b7a9b05993        2 minutes ago       64.6MB
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay, it works! Think of what you can do now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compile Kotlin code to a native binary

&lt;ul&gt;
&lt;li&gt;you don't have to use the CLI compiler - this should work for your Gradle-builds from the IDE as well!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;no need for the JVM, so our Docker image already has &lt;strong&gt;64.6MB&lt;/strong&gt; in size (not more than 600MB)! 🌟&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;And because Docker caches the lower layers (for us it's Ubuntu), there is no actual need to make this smaller. This will be the best result for most people, because if you need a shell or other Ubuntu tools, this image will give you that flexibility. But if you're courious how we could make this smaller, and you're willing to leave some comfort behind: read on!&lt;/p&gt;

&lt;h2&gt;
  
  
  Honey, I Shrunk the Docker Image
&lt;/h2&gt;

&lt;p&gt;Docker offers a way to create &lt;a href="https://docs.docker.com/develop/develop-images/baseimages/#create-a-simple-parent-image-using-scratch" rel="noopener noreferrer"&gt;images from scratch&lt;/a&gt;. So let's do that - we create a file &lt;code&gt;shrinked.Dockerfile&lt;/code&gt;, and we'll build a new Docker image &lt;code&gt;hello_kotlin_shrinked&lt;/code&gt; containing only our Kotlin Native executable!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; scratch&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; hello_world_linux.kexe /hello_world&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["/hello_world"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So let's build and run that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker build &lt;span class="nt"&gt;--tag&lt;/span&gt; hello_kotlin_shrinked &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;--file&lt;/span&gt; shrinked.Dockerfile
&lt;span class="go"&gt;[…]
Successfully tagged hello_kotlin_shrinked:latest

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; hello_kotlin_shrinked
&lt;span class="go"&gt;standard_init_linux.go:211: exec user process caused "no such file or directory"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hmm, that didn't work out. The reason is simple: the Kotlin Native compiler created a dynamically linked binary, meaning this file contains references to some shared libraries (&lt;code&gt;.so&lt;/code&gt; files on Linux, &lt;code&gt;.dll&lt;/code&gt; on Windows). Bummer!&lt;/p&gt;

&lt;p&gt;I couldn't find any option for the &lt;code&gt;kotlinc-native&lt;/code&gt; compiler to build a static, self-contained executable. I played around a little, but I didn't find anything close to how Go creates self-contained binaries. I found this &lt;a href="https://xebia.com/blog/create-the-smallest-possible-docker-container/" rel="noopener noreferrer"&gt;excellent blog post&lt;/a&gt; on how to do this from Go.&lt;/p&gt;

&lt;p&gt;I also asked around in the &lt;a href="https://kotlinlang.slack.com/archives/C3SGXARS6" rel="noopener noreferrer"&gt;#kotlin-native&lt;/a&gt; channel of the Kotlin Slack. I got a reply by Dominic Fischer that there's an &lt;code&gt;-include-binary&lt;/code&gt; option which could do what I want. I didn't come around to try that out yet, but if I do I'll update this post. Thanks, Dominic!&lt;/p&gt;

&lt;p&gt;So, our binary doesn't run because it can't find those shared libraries. What happens if we add those to the Docker image directly? We can find which are needed by using the &lt;code&gt;ldd&lt;/code&gt; tool. Let's use our previously created, Ubuntu-based Docker image to debug this. We're overriding the entrypoint to be a &lt;code&gt;bash&lt;/code&gt;. Also we can mount the current working directory to &lt;code&gt;/host&lt;/code&gt; if we need this later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;--volume&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;:/foo &lt;span class="nt"&gt;--entrypoint&lt;/span&gt; /bin/bash &lt;span class="nt"&gt;-it&lt;/span&gt; hello_kotlin
&lt;span class="go"&gt;
&lt;/span&gt;&lt;span class="gp"&gt;root@a34eeb5c7bef:/#&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;ldd /hello_world
&lt;span class="go"&gt;    linux-vdso.so.1 (0x00007ffc187bd000)
&lt;/span&gt;&lt;span class="gp"&gt;    libdl.so.2 =&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;/lib/x86_64-linux-gnu/libdl.so.2 &lt;span class="o"&gt;(&lt;/span&gt;0x00007f1a39a92000&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;    libm.so.6 =&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;/lib/x86_64-linux-gnu/libm.so.6 &lt;span class="o"&gt;(&lt;/span&gt;0x00007f1a396f4000&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;    libpthread.so.0 =&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;/lib/x86_64-linux-gnu/libpthread.so.0 &lt;span class="o"&gt;(&lt;/span&gt;0x00007f1a394d5000&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;    libgcc_s.so.1 =&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;/lib/x86_64-linux-gnu/libgcc_s.so.1 &lt;span class="o"&gt;(&lt;/span&gt;0x00007f1a392bd000&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;    libc.so.6 =&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;/lib/x86_64-linux-gnu/libc.so.6 &lt;span class="o"&gt;(&lt;/span&gt;0x00007f1a38ecc000&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;    /lib64/ld-linux-x86-64.so.2 (0x00007f1a39c96000)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, so now we know which shared libraries we need! &lt;code&gt;linux-vdso.so.1&lt;/code&gt; is a special library - it is directly provided by the Linux kernel. So we only need to care about the others.&lt;/p&gt;

&lt;p&gt;I copied the entire Ubuntu content over to my host, and I created 2 empty directories &lt;code&gt;lib&lt;/code&gt; and &lt;code&gt;lib64&lt;/code&gt; and copied over only the mentioned files and symbolic links from the original Ubuntu image. Let's see how this looks:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl8yqny3392c0w4s1dbr.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffl8yqny3392c0w4s1dbr.png" alt="File structure of lib and lib64 directories" width="734" height="904"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can modify our &lt;code&gt;shrinked.Dockerfile&lt;/code&gt; and add those libs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; scratch&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; lib /lib&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; lib64 /lib64&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; hello_world_linux.kexe /hello_world&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["/hello_world"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see if we can now build and run this!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker build &lt;span class="nt"&gt;--tag&lt;/span&gt; hello_kotlin_shrinked &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;--file&lt;/span&gt; shrinked.Dockerfile
&lt;span class="go"&gt;[…]
Successfully tagged hello_kotlin_shrinked:latest

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker run &lt;span class="nt"&gt;--rm&lt;/span&gt; hello_kotlin_shrinked
&lt;span class="go"&gt;Hello, World!

&lt;/span&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;docker images &lt;span class="s1"&gt;'hello_kotlin*'&lt;/span&gt;
&lt;span class="go"&gt;REPOSITORY              TAG                 IMAGE ID            CREATED              SIZE
hello_kotlin_shrinked   latest              a66abb1efa53        About a minute ago   4.54MB
hello_kotlin            latest              47b7a9b05993        3 hours ago          64.6MB
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wow, that's cool - we now have a Docker image of &lt;strong&gt;4.54MB&lt;/strong&gt; in size that runs a Kotlin Native binary successfully. Party time! 🎉🎆🎄&lt;/p&gt;

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

&lt;p&gt;We looked at how to cross-compile a Kotlin program for Linux. Then we created a Docker image, using that program as an entrypoint. By leaving the JVM behind, we could already produce a Docker image of 64.6MB, most of which coming from the Ubuntu image itself.&lt;/p&gt;

&lt;p&gt;Then, we entered the danger zone and looked at how we can strip most things of Ubuntu away. We included only the necessary libraries, and that way we got the image down to 4,54MB.&lt;/p&gt;

&lt;p&gt;Some final note: If you compile your own (real) Kotlin Native program, you probably need more files than you might think. Using https connections? I bet you'll need the SSL root certificates provided in each Linux distro. Using some Kotlin Native libs? Maybe some of those add references to other native &lt;code&gt;.so&lt;/code&gt; libs, and your program won't start otherwise. So always test sensibly!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you liked this post, please give it a ❤️ and follow me on &lt;a href="https://twitter.com/mreichelt" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;!&lt;/strong&gt;&lt;br&gt;
I would love to know more if you use this somewhere. Also, I'm pretty sure there must be some tools / tricks out there that would help in this matter. If you know something I don't: please write! 🙌&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@auntneecey" rel="noopener noreferrer"&gt;Denise Johnson&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/m4eISQg0e0E" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>docker</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
