<?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 Allan Caine on Medium]]></title>
        <description><![CDATA[Stories by Allan Caine on Medium]]></description>
        <link>https://medium.com/@adcaine?source=rss-d9b97d7a9e88------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DIYJfh2UH0W73J0uitr_zA.jpeg</url>
            <title>Stories by Allan Caine on Medium</title>
            <link>https://medium.com/@adcaine?source=rss-d9b97d7a9e88------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 06:43:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adcaine/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[The Perils of Inheritance]]></title>
            <link>https://medium.com/kotlin-academy/the-perils-of-inheritance-698b777fa773?source=rss-d9b97d7a9e88------2</link>
            <guid isPermaLink="false">https://medium.com/p/698b777fa773</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[final]]></category>
            <category><![CDATA[inheritance]]></category>
            <category><![CDATA[delegation]]></category>
            <category><![CDATA[open]]></category>
            <dc:creator><![CDATA[Allan Caine]]></dc:creator>
            <pubDate>Wed, 12 Dec 2018 07:41:17 GMT</pubDate>
            <atom:updated>2020-05-20T11:21:36.805Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FRh8-w0-heOF2k0l" /><figcaption>Photo by <a href="https://unsplash.com/@helloquence?utm_source=medium&amp;utm_medium=referral">Helloquence</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This article will show the perils of class inheritance. This article will illustrate the alternative to class inheritance — composition. After reading this article, you will understand why Kotlin makes all classes final by default. This article will explain why you should not make a Kotlin class open unless there’s a good reason to do so.</p><p>Suppose that we have the following interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc7a173766cf47b971b5409c3366787c/href">https://medium.com/media/fc7a173766cf47b971b5409c3366787c/href</a></iframe><p>Further, BaseInsert is an implementation of the Insertable&lt;Number&gt; interface. BaseInsert is open. So, we can extend BaseInsert.</p><p>We want CountingInsertto extend BaseInsert. Each time the code inserts a Number, the code must increase the variablecount by one. So, we have:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e260bcdc6f1e3bc9132ee813cf4351b9/href">https://medium.com/media/e260bcdc6f1e3bc9132ee813cf4351b9/href</a></iframe><p>This implementation ought to work. Line 8 increases count by one; line 13 by the number of variable arguments.</p><p>The code does not work as expected. See line 7 of Fig. 3 below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cde2094d0ad89c5ad560e21141fa5a9/href">https://medium.com/media/2cde2094d0ad89c5ad560e21141fa5a9/href</a></iframe><p>The problem is at line 10 of Fig. 4 below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0e9478f28439bf5b3ddb496d9c3f25d/href">https://medium.com/media/d0e9478f28439bf5b3ddb496d9c3f25d/href</a></iframe><p>The BaseInsert.insertAll function is a convenience function. The insertAll function calls insert for each item in the vararg list. The CountingInsert class double counted the insertion of integers 3 and 4. CountingInsert.insertAll executed the statement count++ twice; the statement count += items.size once. The CountingInsert.insertAll function increased count by four instead of two.</p><p>Are there any alternatives? Yes. We can change the code, or we can use composition.</p><p>Changing the code seems to be an obvious solution. Suppose we are allowed to change the base class. We can change the implementation of BaseInsert.insertAll to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d63b4c9048a9d18eee180bc53f5a4512/href">https://medium.com/media/d63b4c9048a9d18eee180bc53f5a4512/href</a></iframe><p>This implementation avoids calling BaseInsert.insert() , the source of our trouble.</p><p>Suppose we don’t have access to the BaseInsert class. Then we can remove the override to insertAll(). See Fig. 6:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d21e9e4312ad305e8c9dcb02a1e601d9/href">https://medium.com/media/d21e9e4312ad305e8c9dcb02a1e601d9/href</a></iframe><p>Resolving the problem through code changes is fragile. The CountingInsert class relies upon the implementation details of BaseInsert. Is there an even better way? Yes, let’s use composition.</p><p>Fig. 7 is the implementation by composition:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99d84668d0d2cb2f07b5a104aa514930/href">https://medium.com/media/99d84668d0d2cb2f07b5a104aa514930/href</a></iframe><p>Assume that the BaseInsert class uses the Fig. 4 implementation. When we test the InsertDelegation class, the result is correct. See line 15 of Fig. 8 below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51659f6bc7d0650e53b15eba81d67973/href">https://medium.com/media/51659f6bc7d0650e53b15eba81d67973/href</a></iframe><p>Comparing Figs. 2 and 7, the implementations of insert and insertAll are similar. See Fig 9. below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/256d1bd20d5dcb0cf7eb14f673146673/href">https://medium.com/media/256d1bd20d5dcb0cf7eb14f673146673/href</a></iframe><p>The comparable methods are the same with one exception. Inheritance uses super; composition uses insertable. Compare lines 3 versus 12; and 7 versus 16 of Fig. 8. The delegation pattern leaves the insertion task to the insertable. The CompositionInsert class increments the count variable. By contrast, inheritance breaks the encapsulation of the BaseInsert class.</p><p>What was the root cause of the problem? Suppose that BaseInsert was not open. See line 1 of Fig. 4. If BaseInsert were final, then the Kotlin compiler would flag the code in Figs. 2 and 5 as errors. Only the solution in Fig. 7 would be workable. When we make the BaseInsert class final, the encapsulation of BaseInsert is not broken.</p><p>Kotlin understands the perils of inheritance. Kotlin forbids inheritance unless the developer marks the class as open. Conclusion: in general, Kotlin classes should be final unless there’s a good reason to make the class open.</p><p>Do you need a Kotlin workshop? Visit <a href="https://www.kt.academy/">our website</a> to see what we can do for you.</p><p>To be up-to-date with great news on Kt. Academy, <a href="https://kotlin-academy.us17.list-manage.com/subscribe?u=5d3a48e1893758cb5be5c2919&amp;id=d2ba84960a">subscribe to the newsletter</a>, <a href="https://twitter.com/ktdotacademy">observe Twitter</a> and follow on medium.</p><figure><a href="http://eepurl.com/diMmGv"><img alt="" src="https://cdn-images-1.medium.com/max/700/1*54OqlYA4etu7wfpmMP5TKQ.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=698b777fa773" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kotlin-academy/the-perils-of-inheritance-698b777fa773">The Perils of Inheritance</a> was originally published in <a href="https://medium.com/kotlin-academy">kt.academy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin’s Nothing: Its Usefulness in Generics]]></title>
            <link>https://medium.com/kotlin-academy/kotlins-nothing-its-usefulness-in-generics-5076a6a457f7?source=rss-d9b97d7a9e88------2</link>
            <guid isPermaLink="false">https://medium.com/p/5076a6a457f7</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[nothing]]></category>
            <category><![CDATA[generics]]></category>
            <dc:creator><![CDATA[Allan Caine]]></dc:creator>
            <pubDate>Wed, 25 Apr 2018 06:11:01 GMT</pubDate>
            <atom:updated>2018-11-22T17:17:51.532Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wzOHXCAS6ywoQJpo." /><figcaption>Image credit: <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>This article explores the usefulness of Kotlin’s Nothing type in generics. We look at its relationship to Java. To take a concrete example, let’s look at a linked list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F6K2liBcORMFsEyGUsrJLg.png" /><figcaption>Fig. 1: A LinkedList</figcaption></figure><p>A LinkedList wraps some type T . The linked list can either be</p><ol><li>A Node&lt;T&gt; with two properties: a payload of T and a next of type LinkedList&lt;T&gt;; or</li><li>An EmptyList.</li></ol><p>A sealed class enforces that the LinkedList is either type 1 or type 2.</p><p>We can readily code up the sealed class and the Node&lt;T&gt; as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/803ca5a99b750c9e48b1863b2168b49a/href">https://medium.com/media/803ca5a99b750c9e48b1863b2168b49a/href</a></iframe><p>Coding up the empty list is a bit more challenging. All empty lists are the same. So, an empty list is an object. EmptyList must also be a subclass of LinkedList&lt;T&gt;. We might try to write</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1e6a26534ca882da38553311c89816a/href">https://medium.com/media/e1e6a26534ca882da38553311c89816a/href</a></iframe><p>Kotlin objects cannot have type parameters. The above code will not compile. Instead, we might try to remove the EmptyList’s type parameter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f3c0b06bb504905c4da953798a17bf3/href">https://medium.com/media/9f3c0b06bb504905c4da953798a17bf3/href</a></iframe><p>The code still does not compile. The reference to T in line 5 is unresolved. We must stipulate a concrete type for T.</p><p>T is the type of the payload wrapped by a node. See Fig. 1 above. By contrast, an empty list wraps no payload. So, the proper coding is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba8e52751d99fd571bb74427f9acede6/href">https://medium.com/media/ba8e52751d99fd571bb74427f9acede6/href</a></iframe><p>What is the Kotlin Nothing type? From Android Studio, select Tools -&gt; Kotlin -&gt; Kotlin REPL. In the REPL window, enter and run the command println(Nothing::class.java). The result is</p><pre>println(Nothing::class.java)<br>class java.lang.Void</pre><p>Kotlin’s Nothing type is backed by Java’s Void type. Kotlin’s Nothing type indicates the <em>absence</em> of type.</p><p>As a side point, why can’t we define a Kotlin function to return Nothing? For example, this code will not compile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9207943166f6a2ff0abff32d4d615fa/href">https://medium.com/media/c9207943166f6a2ff0abff32d4d615fa/href</a></iframe><p>The constructor for Nothing is private. Compare the above code to the Java equivalent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f1190c24ec30ae0d418479b71d2c767/href">https://medium.com/media/1f1190c24ec30ae0d418479b71d2c767/href</a></iframe><p>Java’s Void class has a private constructor. Void cannot be instantiated. We cannot return a Void. So, it seems reasonable that we cannot return a Nothing in Kotlin.</p><p>We have shown that Kotlin’s Nothing type is backed by Java’s Void type. Kotlin’s Nothing type is used to indicate that a generic type wraps no type. The type is absent.</p><p>To be up-to-date with great news on Kt. Academy, <a href="https://kotlin-academy.us17.list-manage.com/subscribe?u=5d3a48e1893758cb5be5c2919&amp;id=d2ba84960a">subscribe to the newsletter</a>, <a href="https://twitter.com/ktdotacademy">observe Twitter</a> and follow us on medium.</p><figure><a href="http://eepurl.com/diMmGv"><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*uDqv_d5NZnPUJA0FeZqhqQ.png" /></a></figure><p>If you like it, remember to <strong>clap</strong>. Note that if you hold the clap button, you can leave more claps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5076a6a457f7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kotlin-academy/kotlins-nothing-its-usefulness-in-generics-5076a6a457f7">Kotlin’s Nothing: Its Usefulness in Generics</a> was originally published in <a href="https://medium.com/kotlin-academy">kt.academy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Living without Null]]></title>
            <link>https://medium.com/kotlin-academy/living-without-null-da9b695c908b?source=rss-d9b97d7a9e88------2</link>
            <guid isPermaLink="false">https://medium.com/p/da9b695c908b</guid>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[null]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[sealed-classes]]></category>
            <dc:creator><![CDATA[Allan Caine]]></dc:creator>
            <pubDate>Sat, 07 Apr 2018 23:22:08 GMT</pubDate>
            <atom:updated>2018-11-22T17:21:14.367Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*reNh2_WGq-CqM41O." /><figcaption>Image credit <a href="https://unsplash.com/@jesuskiteque?utm_source=medium&amp;utm_medium=referral">Jesus Kiteque</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When writing code, we can usually avoid using null. It represents a state.</p><p>In Kotlin, there are many ways to avoid using null. In this article, we will show how to avoid using null to represent the <em>empty</em> state.</p><p>For example, the typical Java definition of a linked list is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c32c775c5b52684aa794d1a5e52558c/href">https://medium.com/media/5c32c775c5b52684aa794d1a5e52558c/href</a></iframe><p>In this Java implementation, null represents the empty list. Using null works. Yet, we incur all the hazards associated with the use of nulls. Null pointer exceptions are one hazard of using null. Is there a better way? Let’s explore Kotlin.</p><p>Kotlin can emulate the Java implementation of a linked list:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa74c2b26e77fce1183a2c45b950a156/href">https://medium.com/media/aa74c2b26e77fce1183a2c45b950a156/href</a></iframe><p>Like its Java counterpart, this implementation comes at a price. It uses the nullable LinkedList&lt;T&gt;? . Can we improve our code further? The answer is yes.</p><p>A naïve approach to removing the nullable type LinkedList&lt;T&gt;? from the data class involves removing the question mark from the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1c9a2854bcb11a0a23ac8a219913575/href">https://medium.com/media/a1c9a2854bcb11a0a23ac8a219913575/href</a></iframe><p>The definition of the data class on line 3 is impractical. Also, lines 6 and 7 will not compile. Line 3 is saying that any linked list has a payload T and a reference to another non-null linked list. In other words, the linked list has an infinite length.</p><p>The definition above has an error. It does not take into account the empty list. Let’s define a linked list as follows:</p><p>A linked list is either:</p><ol><li>A node with two properties: a non-null payload of type T and a non-null linked list; or</li><li>A terminal object.</li></ol><p>We will use a sealed class to enforce that a linked list is either type 1 or type 2 <em>and nothing more</em>. To begin, let’s define the Node&lt;T&gt;:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54602f32633215623bc34a2630e83483/href">https://medium.com/media/54602f32633215623bc34a2630e83483/href</a></iframe><p>We will use Kotlin’s object to define Terminal, because all Terminals are the same. Terminal is a singleton.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53f5391b1de812a3a15e3257f64cb64d/href">https://medium.com/media/53f5391b1de812a3a15e3257f64cb64d/href</a></iframe><p>For the sake of convenience, the default value of the next property is Terminal. See line 4. We can code up a LinkedList without using the Terminal object. See line 9.</p><p>Let’s explore a few practical uses of our definition of a linked list. For example, we can define the toString() method as follows:</p><ol><li>If LinkedList is a Node&lt;T&gt; then return the concatenation of the payload and next.</li><li>If LinkedList is a Terminal then return the empty string.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60f7303183d9f814e4a795939b711222/href">https://medium.com/media/60f7303183d9f814e4a795939b711222/href</a></iframe><p>Size is another example. The definition of size is:</p><ol><li>If LinkedList is a Node&lt;T&gt;, then the size is 1 plus the size of the next property of the node.</li><li>If LinkedList is a Terminal, then the size is zero.</li></ol><p>Using Kotlin’s when as an expression, we can define the size recursively as</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dea06cd2a394a67f71f92f837513fae4/href">https://medium.com/media/dea06cd2a394a67f71f92f837513fae4/href</a></iframe><p>We defined the linked list as a sealed class. So, the when in line 1 is an expression. If when is an expression, then when must be exhaustive: Node&lt;T&gt;and Terminal.</p><p>As an alternative, we can place the definition of sizeRecursively()within the sealed class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cd4b21c0c9bdfae0cc43b82de679cb4/href">https://medium.com/media/5cd4b21c0c9bdfae0cc43b82de679cb4/href</a></iframe><p>The function sizeRecursively() may not be very efficient. It is not tail recursive. So, an iterative implementation of sizeIteratively() may be better:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/116b53cad5b6be4885579040709f87a6/href">https://medium.com/media/116b53cad5b6be4885579040709f87a6/href</a></iframe><p>We have shown that null represents a state. Using nulls involves dangers like null pointer exceptions. We have shown that we can enumerate all the possible states of a linked list by using a sealed class.</p><p>By using a sealed class as an enumeration of all possible states of an object, the code does not use null.</p><p>To be up-to-date with great news on Kt. Academy, <a href="https://kotlin-academy.us17.list-manage.com/subscribe?u=5d3a48e1893758cb5be5c2919&amp;id=d2ba84960a">subscribe to the newsletter</a>, <a href="https://twitter.com/ktdotacademy">observe Twitter</a> and follow us on medium.</p><figure><a href="http://eepurl.com/diMmGv"><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*uDqv_d5NZnPUJA0FeZqhqQ.png" /></a></figure><p>If you like it, remember to <strong>clap</strong>. Note that if you hold the clap button, you can leave more claps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da9b695c908b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kotlin-academy/living-without-null-da9b695c908b">Living without Null</a> was originally published in <a href="https://medium.com/kotlin-academy">kt.academy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Eliminate Boilerplate using Kotlin Extensions]]></title>
            <link>https://adcaine.medium.com/eliminate-boilerplate-using-kotlin-extensions-a10d0063ce17?source=rss-d9b97d7a9e88------2</link>
            <guid isPermaLink="false">https://medium.com/p/a10d0063ce17</guid>
            <category><![CDATA[eliminateboilerplate]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[extension-functions]]></category>
            <dc:creator><![CDATA[Allan Caine]]></dc:creator>
            <pubDate>Mon, 25 Dec 2017 15:25:59 GMT</pubDate>
            <atom:updated>2017-12-25T15:25:59.412Z</atom:updated>
            <content:encoded><![CDATA[<p>Kotlin’s extensions can be used to reduce boiler-plate in your code. To illustrate, let’s consider a simple custom View. The custom view, MyView, subclasses TextView. Let us suppose that the custom view has an associated styleable xml</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adcbd4b8994547449f53c99f57decfea/href">https://medium.com/media/adcbd4b8994547449f53c99f57decfea/href</a></iframe><p>The custom view can be incorporated into a layout xml using the attribute theText:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a542914939a2bbb75be8bc9e8e21746/href">https://medium.com/media/3a542914939a2bbb75be8bc9e8e21746/href</a></iframe><p>The process of getting an attribute from an xml is generally done in the same way every time:</p><ol><li>The TypedArray is obtained fromContext.obtainStyledAttributes(…)</li><li>Methods of the TypedArray are used to assign attribute’s values to class properties in a try block</li><li>The TypedArray is recycled in a finally block</li></ol><p>Without simplification using Kotlin extensions, the implementation of MyView is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b439f46d5ac1a2a9fec94fe2d4ddee2/href">https://medium.com/media/2b439f46d5ac1a2a9fec94fe2d4ddee2/href</a></iframe><p>Line 5 corresponds to Step 1; line 8 step 2; and line 10 step 3.</p><p>We can begin simplification by observing that only line 8 in the try/finally block is truly unique to MyView. The rest is boiler plate. We can relegate the try/finally block to a Kotlin extension of TypedArray:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/969b0fc72cd52287b34660313d09a8f8/href">https://medium.com/media/969b0fc72cd52287b34660313d09a8f8/href</a></iframe><p>We can simplify the MyView class as</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/502ad1a0e72c24513d19c5936da1d8b5/href">https://medium.com/media/502ad1a0e72c24513d19c5936da1d8b5/href</a></iframe><p>The line which reads</p><pre>text = getString(R.styleable.MyView_theText)</pre><p>is the block mentioned in lines 1 and 3 of the ViewCreateUtils. The code which is unique to MyView is passed to TypedArray.use(block: TypedArray.() -&gt; Unit) as a lambda function.</p><p>Further simplification is possible. Obtaining the TypedArray from the Context is done in the same way for custom views. We can define an extension on Context.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b9125aac76dee9f58ab0ece450af671/href">https://medium.com/media/9b9125aac76dee9f58ab0ece450af671/href</a></iframe><p>The final simplification of MyView is</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a746bda0be1e8943db245c6c8ac45d7/href">https://medium.com/media/4a746bda0be1e8943db245c6c8ac45d7/href</a></iframe><p>Remarkably, the TypedArray is not directly mentioned in the simplified code. The extension function getStyledAttributes does the work of obtaining the TypedArray for us. The try/finally block is not mentioned either. The use extension function runs line 6 above for us in a try block. Finally, the TypedArray is recycled in a finally block for us.</p><p>By using extension functions and lambdas, our initializer has gone from</p><pre><strong>init </strong>{<br>    <strong>val </strong>typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.<em>MyView</em>, 0, 0)<br>    <br>    <strong>try </strong>{<br>        <em>text </em>= typedArray.getText(R.styleable.<em>MyView_theText</em>)<br>    } <strong>finally </strong>{<br>        typedArray.recycle()<br>    }</pre><p>to</p><pre><strong>init </strong>{<br>    context.<em>getStyledAttributes</em>(attributeSet, R.styleable.<em>MyView</em>) <strong>{<br>        </strong><em>text </em>= getString(R.styleable.<em>MyView_theText</em>)<br>    <strong>}</strong></pre><p>All of the boiler plate code has been eliminated. The simplified init block only contains the essential code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a10d0063ce17" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>