<?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 Giuseppe Filograno on Medium]]></title>
        <description><![CDATA[Stories by Giuseppe Filograno on Medium]]></description>
        <link>https://medium.com/@filograno?source=rss-2c869cf8304------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*x4lbLx4NG7JhOgD7MUiRpA.jpeg</url>
            <title>Stories by Giuseppe Filograno on Medium</title>
            <link>https://medium.com/@filograno?source=rss-2c869cf8304------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 16:32:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@filograno/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[Deconstructing values in Kotlin]]></title>
            <link>https://medium.com/@filograno/deconstructing-values-in-kotlin-2349c8af55cc?source=rss-2c869cf8304------2</link>
            <guid isPermaLink="false">https://medium.com/p/2349c8af55cc</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-languages]]></category>
            <dc:creator><![CDATA[Giuseppe Filograno]]></dc:creator>
            <pubDate>Sun, 15 Mar 2020 18:54:03 GMT</pubDate>
            <atom:updated>2020-03-18T08:19:26.072Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6sS5kPGMTySYrjCUwAouqA.jpeg" /><figcaption>Piani di Artavaggio, Lecco (Italy)</figcaption></figure><blockquote>Deconstructing values is the possibility to get a particular value from a function that usually returns a class.</blockquote><p>It’s an important feature of the Kotlin programming language and it’s often called <em>destructuring declaration</em> too.</p><p>It’s so important because it guarantees to iterate over keys and values of a map. Suppose that we have a map:</p><pre>val countries = <em>mapOf</em>(&quot;Italy&quot; <em>to </em>&quot;Rome&quot;, &quot;Spain&quot; <em>to </em>&quot;Madrid&quot;, &quot;France&quot; <em>to </em>&quot;Paris&quot;)</pre><p>Now we can iterate over this map by using a simple for loop:</p><pre>for ((country, capital) in countries) {<br>    <em>println</em>(&quot;$country - $capital&quot;)<br>}</pre><p>We can print out the country and capital of the Country map thanks to the deconstruction.</p><p>Now let’s have a look at how this feature works for Pairs.</p><p>In Kotlin a Pair is a special class that can contains a couple of values. To access to these values is possible by using the <strong>first</strong> and <strong>second</strong> properties. See the function below:</p><pre>fun getZeroCoordinate(): Pair&lt;Float, Float&gt; {<br>    return Pair(0.0F, 0.0F)<br>}</pre><p>Now I can access to the first and second element of the function in this way:</p><pre>val zero = <em>getZeroCoordinate</em>()<br><em>println</em>(&quot;${zero.first} - ${zero.second}&quot;)</pre><p>To access these elements it’s also possible to use <strong>component1()</strong> and <strong>component2()</strong> method:</p><pre>val zero = <em>getZeroCoordinate</em>()<br><em>println</em>(&quot;${zero.component1()} - ${zero.component2()}&quot;)</pre><p>So what are these strange <strong>component1()</strong> and <strong>component2()</strong> methods? Well these methods are created for all data classes and allow us to deconstruct values 💣💥🤯.</p><p>Under the hood a Pair is a Kotlin <strong>data class</strong> and this is the reason why it has these methods.</p><p>At this point, let me show you how the deconstruction works for data classes.</p><p>Let me create a Person data class:</p><pre>data class Person(val name: String, val surname: String)</pre><p>Now I can use its constructor to get an instance of this class:</p><pre>val mySelf = Person(&quot;Giuseppe&quot;, &quot;Filograno&quot;)<br><em>println</em>(&quot;${mySelf.name} - ${mySelf.surname}&quot;)</pre><p>or I can deconstruct its properties:</p><pre>val (myName, mySurname) = Person(&quot;Giuseppe&quot;, &quot;Filograno&quot;)<br><em>println</em>(&quot;$myName - $mySurname&quot;)</pre><p>This is possible because data classes automatically creates a <em>component1()</em> and <em>component2()</em> methods.</p><p>Generally these methods are called <strong>componentN()</strong> functions.<strong> 😱</strong></p><p>But it’s possible to deconstruct parameters for a class (instead of using data class) ?</p><p>Let’s define a class and try to deconstruct its properties:</p><pre>class Country(val name: String, val capital: String)</pre><pre>val (name, capital) = Country(&quot;Italy&quot;, &quot;Rome&quot;)</pre><p>The Kotlin compiler gets these errors:</p><blockquote>Destructuring declaration initializer of type Country must have a ‘component1()’ function</blockquote><blockquote>Destructuring declaration initializer of type Country must have a ‘component2()’ function</blockquote><p>Which means that the Country class does not have the the <em>componentN()</em> functions and cannot deconstruct the <em>name</em> and <em>capital</em> properties.</p><p>How can we fix this? We can declare the <em>componentN()</em> functions in the Country class remembering to use the <strong>operator</strong> keyword:</p><pre>class Country(val name: String, val capital: String) {<br>    operator fun component1(): String = name<br>    operator fun component2(): String = capital<br>}</pre><p>In this way I’m defining how the deconstruction will work on this class specifying that the first component of the deconstruction will be the name of the Country, while the second will be the capital. 😜</p><p>Ok, it’s almost done! Now let’s come back to our maps and let me explain how the deconstruction is possible on maps.</p><p>The Kotlin standard library has these Map extension functions:</p><pre>operator fun &lt;K, V&gt; Map.Entry&lt;K, V&gt;.component1() = getKey()<br>operator fun &lt;K, V&gt; Map.Entry&lt;K, V&gt;.component2() = getValue()</pre><p>that are super useful to get back the key and value of a map in a for loop.</p><p>If you wanna go deeper in this topic you can read the Kotlin official documentation at this link: <a href="https://kotlinlang.org/docs/reference/multi-declarations.html">https://kotlinlang.org/docs/reference/multi-declarations.html</a></p><p>Thanks for reading this article. I’m curious to listen to what do you think about it, so feel free to comment it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2349c8af55cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>