<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexandre Crettet on Medium]]></title>
        <description><![CDATA[Stories by Alexandre Crettet on Medium]]></description>
        <link>https://medium.com/@alexandrecrettet?source=rss-ff7d9d2312b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*drz2UBAQPOgSh2HBGwx-Yw.jpeg</url>
            <title>Stories by Alexandre Crettet on Medium</title>
            <link>https://medium.com/@alexandrecrettet?source=rss-ff7d9d2312b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 09:52:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexandrecrettet/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kotlin when Expression]]></title>
            <link>https://medium.com/@alexandrecrettet/kotlin-unleashed-when-expression-53b4d69173d3?source=rss-ff7d9d2312b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/53b4d69173d3</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[kotlindev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin-beginners]]></category>
            <dc:creator><![CDATA[Alexandre Crettet]]></dc:creator>
            <pubDate>Wed, 03 Apr 2019 09:52:28 GMT</pubDate>
            <atom:updated>2019-05-06T22:24:41.833Z</atom:updated>
            <content:encoded><![CDATA[<p>As a software lead developer, I’ve seen a lot of Kotlin developers not knowing the true power of the Kotlin’s when expression. This article is intended to cover its usages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eDS1mY9Iyw7JXpS-deruYw.jpeg" /></figure><h4>Basic usage</h4><p>Below is the most basic usage of when expression:</p><pre>fun myFunction(valueToTest: Any) {<br>    when (valueToTest) {<br>        is MyType1 -&gt; doSomething1()<br>        is MyType2 -&gt; doSomething2()<br>    }<br>}</pre><p>The compiler gives us a warning because this implementation is non-exhaustive, which means that we do not treat all the possible cases. &quot;valueToTest&quot; being of type Any, we have infinite cases to handle.</p><p>To remove the compiler&#39;s warning, we can just add the else case:</p><pre>fun myFunction(valueToTest: Any) {<br>    when (valueToTest) {<br>        is MyType1 -&gt; doSomething1()<br>        is MyType2 -&gt; doSomething2()<br>        else -&gt; doSomethingElse()<br>    }<br>}</pre><h4>From warning to error</h4><p>A specificity of Kotlin is that some keywords return values. This is the case of if/else and when. But what is the compiler supposed to do when we want to get a value out of a non-exhaustive when? Well, I&#39;m sure you already have it. The warning we had in our first example becomes an error, which means the program does not compile anymore. Let’s take our first example:</p><pre>fun myFunction(valueToTest: Any): Boolean {<br>    return when (valueToTest) {<br>        is MyType1 -&gt; true<br>        is MyType2 -&gt; true<br>    }<br>}</pre><p>Here I added a return type to myFunction and the return keyword just before the when expression. To make this code compile, we have to add the else case as we did to remove the warning:</p><pre>fun myFunction(valueToTest: Any): Boolean {<br>    return when (valueToTest) {<br>        is MyType1 -&gt; true<br>        is MyType2 -&gt; true<br>        else -&gt; false<br>    }<br>}</pre><p>This example compiles. But I personally don&#39;t like much this kind of code. By passing a parameter Any, we lose the killer feature this article is all about.</p><h4>Killer feature, you said?</h4><p>Alright. Let&#39;s add a bit more code to our example. We need a sealed class or an enum class (behavior with when is the same):</p><pre>enum class MyEnum {<br>    CASE1,<br>    CASE2<br>}</pre><p>MyEnum contains two cases, which by the way are Kotlin objects (singletons). So we can have an exhaustive when without else case:</p><pre>fun myFunction(valueToTest: MyEnum): Boolean {<br>    return when (valueToTest) {<br>        CASE1 -&gt; true<br>        CASE2 -&gt; true<br>    }<br>}</pre><p>Here it is! This compiles perfectly. But I know what you are wondering, why is this code pretty nice? Well, it is all about maintaining it. Imagine if a newbie developer arrives on the project we are working on. If we ask him to add a new case, CASE3, what would happen?</p><pre>enum class MyEnum {<br>    CASE1,<br>    CASE2,<br>    CASE3<br>}</pre><pre>fun myFunction(valueToTest: MyEnum): Boolean {<br>    return when (valueToTest) {<br>        CASE1 -&gt; true<br>        CASE2 -&gt; true<br>    }<br>}</pre><p>The code contained in myFunction would not compile anymore! So our newbie knows in seconds where he needs to handle the new case he has just created.</p><h4>An Android example</h4><p>In this example, we will implement a sealed class which could help us to handle the different types of strings we can meet in Android. A string can be a resource (which is of type Int and refers to a resource id) or a plain string.</p><pre>sealed class AndroidString {<br>    data class Resource(val resource: Int) : AndroidString()<br>    data class Plain(val str: String) : AndroidString()</pre><pre>    fun toString(resources: Resources): String {<br>        return when (this) {<br>            is AndroidString.Resource -&gt; resources.getString(resString)<br>            is AndroidString.Plain -&gt; str<br>        }<br>    }<br>}</pre><p>And voilà! This example shows how to create clean code with enums/sealed classes and the when expression. In a near future, we could want to add another class to our AndroidString sealed class, for example to handle the plural strings case.</p><h4>Conclusion</h4><p>We’ve seen how useful the when expression can be to maintain way faster and easier applications. This is a good way to avoid bugs when the programs we are working on grows, as long as we avoid as much as possible else cases so we can make the compiler show errors when needed.</p><p>Thanks for reading!</p><figure><a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_image"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PYxBP_WV7LI6zc-zdUkNqQ.png" /></a></figure><p>📝 Read this story later in <a href="https://usejournal.com/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_read_later_text">Journal</a>.</p><p>👩‍💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. <a href="https://usejournal.com/newsletter/noteworthy-in-tech/?utm_source=medium.com&amp;utm_medium=noteworthy_blog&amp;utm_campaign=tech&amp;utm_content=guest_post_text">Read the Noteworthy in Tech newsletter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53b4d69173d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Greatness of Kotlin : Android API compatibility]]></title>
            <link>https://medium.com/cheerz-engineering/greatness-of-kotlin-android-api-compatibility-71d0b582a9f0?source=rss-ff7d9d2312b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/71d0b582a9f0</guid>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Alexandre Crettet]]></dc:creator>
            <pubDate>Tue, 18 Jul 2017 16:39:30 GMT</pubDate>
            <atom:updated>2019-01-11T16:11:15.266Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N6ryDMYy1O1J2l79d7zTsA.png" /></figure><p>A great thing about Kotlin is how its developers have taken into account the nicest features of other languages and added them to Kotlin. One of these nice features is <a href="https://kotlinlang.org/docs/reference/extensions.html"><em>extension functions</em></a><em> </em>which allows us to share code very easily between different parts of our programs.</p><h4>Introduction</h4><p>Sometimes, the Android Support Library provides compatibility helpers (like ViewCompat or ContextCompat) but for few methods, you need to handle the different cases on your own.</p><h4>The Java way</h4><p>If you have ever worked with Java, you probably used the following pattern to handle Android API compatibility:</p><pre>public class DragCompat {</pre><pre>    public static void startDragAndDrop(View view, ClipData data) {<br>        View.DragShadowBuilder dragShadowBuilder = new View.DragShadowBuilder(view);<br>        if (Build.VERSION.<em>SDK_INT </em>&gt;= 24)<br>            view.startDragAndDrop(data, dragShadowBuilder, null, 0);<br>        else<br>            view.startDrag(data, dragShadowBuilder, null, 0);<br>    }<br>}</pre><p>Then you would have to call DragCompat.startDragAndDrop(view, clipData) to start your drag and drop.</p><h4>The Kotlin way</h4><p>In Kotlin, we could make this code look a bit better. In a new file (i.e. DragCompat.kt), we could define a top-level function as follows:</p><pre>fun View.startDragAndDropCompat(data: ClipData) {<br>    val dragShadowBuilder = View.DragShadowBuilder(this)<br>    if (Build.VERSION.<em>SDK_INT </em>&gt;= 24)<br>        startDragAndDrop(data, dragShadowBuilder, null, 0)<br>    else<br>        startDrag(data, dragShadowBuilder, null, 0)<br>}</pre><p>And finally, you can call this brand new method on any android view instance: myView.startDragAndDropCompat(clipData) .</p><h4>Conclusion</h4><p>It does not change a lot the way you call the method, but it allows you to not look for helper methods. The functions you create look like they are directly in the base class (thanks to auto-completion). Moreover, when typing view.startDrag , auto-completion will directly show you 3 methods: the deprecated startDrag(...) , the one you should use for new APIsstartDragAndDrop(...) and finally the one you defined yourself startDragAndDropCompat(...).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71d0b582a9f0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cheerz-engineering/greatness-of-kotlin-android-api-compatibility-71d0b582a9f0">Greatness of Kotlin : Android API compatibility</a> was originally published in <a href="https://medium.com/cheerz-engineering">Cheerz Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Greatness of Kotlin : Inheritance]]></title>
            <link>https://medium.com/cheerz-engineering/greatness-of-kotlin-inheritance-a77ec446a254?source=rss-ff7d9d2312b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a77ec446a254</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Alexandre Crettet]]></dc:creator>
            <pubDate>Wed, 31 May 2017 15:33:07 GMT</pubDate>
            <atom:updated>2019-01-11T16:12:23.827Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iUKB2zSdCmFJTSgP_C6tjA.png" /></figure><p>At the Cheerz company, we have been using Kotlin for a year and half on production and have always been very enthusiastic about it. We now have half of our project written in Kotlin so we want to share what we’ve learned. Here is the first post of our “Greatness of Kotlin” blog post series.</p><p>In this short article, we will talk about the best Kotlin inheritance features we’ve seen so far.</p><h3>Basics about kotlin fields</h3><p>We will first create a basic interface, its purpose will be to hold some text data :</p><pre>interface TextHolder {<br>   val text: String<br>}</pre><p>What is it doing ?</p><p>You will tell me the obvious: it is simply declaring a field. Well, not really. This interface is declaring a getter on a field. It does not provide a setter since it’s a val (immutable field). If you also want to declare a setter, you have to declare this field as a var instead of val.</p><p>Tip: You can verify this by using the Android Studio or Intellij kotlin plugin feature : show kotlin bytecode. Looking at the kotlin bytecode is very powerful, you can understand what each line of code will look like after being compiled. It also renders the bytecode in real time, no need to compile it to see the result of your changes.</p><h3>Implementing an interface</h3><p>Let’s implement the previous interface :</p><pre>class TextHolderImpl : TextHolder {<br>   override val text: String = “myString”<br>}</pre><p>Here we have a basic implementation of a kotlin interface. But we could do many more things with these few lines of code. First of all, maybe we want the have a setter on our text field. Remember what we said earlier ? To generate a setter we need to declare it as a var. Easy job :</p><pre>interface TextHolder {<br>   var text: String<br>}</pre><pre>class TextHolderImpl : TextHolder {<br>   override var text: String = “myString”<br>}</pre><p>We can now modify the text field after its initialization.</p><p>If you want to get deeper in vals and their mutability, you can read this post : <a href="http://blog.danlew.net/2017/05/30/mutable-vals-in-kotlin/">http://blog.danlew.net/2017/05/30/mutable-vals-in-kotlin/</a></p><h3>Handle the setter visibility</h3><p>What if we don’t want the setter to be public ? How can we do this?</p><pre>interface TextHolder {<br>   val text: String<br>}</pre><pre>class TextHolderImpl : TextHolder {<br>   override var text: String = “myString”<br>}</pre><p>With this code, we are exposing the getter in the interface and the getter and setter in the class. However, this is still not so good (at least not exactly what we were looking for) : we can modify our field if we have a TextHolderImpl reference (or by casting it), both of which I tend to see as a bad practice in most cases.</p><p>The code we want needs to tell the compiler we want a private setter.</p><pre>interface TextHolder {<br>   val text: String<br>}</pre><pre>class TextHolderImpl : TextHolder {<br>   override var text: String = “myString”<br>      private set<br>}</pre><p>Here it is ! We can now only modify text inside the TextHolderImpl class.</p><p>Please note that if you set a var in the interface, the setter will be public, so you cannot override it with a private setter.</p><h3>Provide an implementation directly in an interface</h3><p>Some Android programmers are lacking the Java 8 (and soon Java 9) features, since we still are using Java 7 on Android. One of these features is implementing methods directly in interfaces, which Kotlin provides allows as well. Let’s have a look on the implementation :</p><pre>interface TextHolder {<br>   val text: CharSequence<br>     get() = “myCharSequence”</pre><pre>fun provideAnotherText() = “I am another text”<br>}</pre><p>Here we wrote a default implementation of the text field. I also added a method which provides another text. If you try to implement this class, you will see that you don’t need to override the fields and methods which already have a default implementation.</p><p>Note: the function provideAnotherText() does not specify its return type. This is a Kotlin feature, if you use this equals syntaxe, the compiler will automatically find the method returns type (here the type will be a String). This also works with fields.</p><h3>Delegate interface implementation to another class</h3><p>Another great thing you can do with Kotlin inheritance is to delegate a specific implementation to another class (or to a constructor parameter).</p><p>Let’s go back with our first interface and implementation :</p><pre>interface TextHolder {<br>   val text: CharSequence<br>}</pre><pre>class TextHolderImpl : TextHolder {<br>   override var text: String = “”<br>      private set<br>}</pre><p>You might want to create another implementation of the interface TextHolder while you do not want to change the code of it. You might also not directly inherit from TextHolderImpl, since you cannot have multi-inheritance in Kotlin. Also TextHolderImpl is a final class and maybe you cannot make it open. So you should use delegates :</p><pre>class MyClass : TextHolder by TextHolderImpl()</pre><p>And that’s it. You do not need to type anything else, your class MyClass is delegating all the interface methods to the TextHolderImpl() instance.</p><p>You could also give the implementation as a primary constructor parameter :</p><pre>class MyClass(textHolder: TextHolder) : TextHolder by textHolder</pre><h3>Conclusion</h3><p>Knowing Kotlin well makes you write only useful lines of code. This is the way we should all think about Kotlin : each line of code should make enough sense for us to accept the pain of writing it and for your coworkers the pain of reading it. This language has been designed to be particularly clear, concise and readable. If this short article has interested you, I invite you to follow our next posts where we will give a deeper dive into the Greatness of Kotlin !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a77ec446a254" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cheerz-engineering/greatness-of-kotlin-inheritance-a77ec446a254">Greatness of Kotlin : Inheritance</a> was originally published in <a href="https://medium.com/cheerz-engineering">Cheerz Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>