<?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: Sagar</title>
    <description>The latest articles on DEV Community by Sagar (@srdpatel).</description>
    <link>https://dev.to/srdpatel</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%2F180433%2F3449403a-66af-4b9a-9e2a-392cea5bba3a.jpg</url>
      <title>DEV Community: Sagar</title>
      <link>https://dev.to/srdpatel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/srdpatel"/>
    <language>en</language>
    <item>
      <title>Kotlin: Scope functions</title>
      <dc:creator>Sagar</dc:creator>
      <pubDate>Thu, 04 Jun 2020 10:25:21 +0000</pubDate>
      <link>https://dev.to/srdpatel/kotlin-scope-functions-p4o</link>
      <guid>https://dev.to/srdpatel/kotlin-scope-functions-p4o</guid>
      <description>&lt;h1&gt;
  
  
  Prerequisite/Previous articles:
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd"&gt;Kotlin: Function type, higher order function, function literal, lambda expression and an anonymous function&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/srdpatel/kotlin-extension-function-1dd7"&gt;Kotlin: Extension function and Receiver type&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/srdpatel/kotlin-inline-function-olf"&gt;Kotlin: Inline function&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  In a hurry?
&lt;/h1&gt;

&lt;p&gt;You can jump to the &lt;em&gt;&lt;strong&gt;conclusion&lt;/strong&gt;&lt;/em&gt; section and &lt;em&gt;&lt;strong&gt;play the concept&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Recap
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;We can access the receiver type from the business logic of an extension function through the keyword: &lt;em&gt;this&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We can omit the keyword: &lt;em&gt;this&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;If a lambda has only one parameter, we can access that argument using the keyword: &lt;em&gt;it&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why scope functions?
&lt;/h2&gt;

&lt;p&gt;To make the code more concise!&lt;/p&gt;

&lt;h2&gt;
  
  
  How does the scope function make the code more concise?
&lt;/h2&gt;

&lt;p&gt;Let us understand it by an example:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=lgETMgNR4&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you can see, we could avoid an extra variable. Moreover, we can also omit &lt;em&gt;this&lt;/em&gt; keyword as below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=fKscMORoX&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;That was neat and clean. Isn't it?&lt;/p&gt;

&lt;p&gt;Let us understand our first scope function: &lt;em&gt;with&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  With
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;receiver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;block&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 us examine the definition.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Inline&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We use the &lt;em&gt;inline&lt;/em&gt; keyword for the higher order function to avoid a function object creation, memory allocation and runtime overhead.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;T, R&amp;gt; with(receiver: T, block: T.() -&amp;gt; R): R&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is a generic function.&lt;/li&gt;
&lt;li&gt;It can have anything as the first parameter. We can pass any instance as the first argument.&lt;/li&gt;
&lt;li&gt;Second parameter is a function type, named as &lt;code&gt;block&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;block&lt;/code&gt; is an extension function.&lt;/li&gt;
&lt;li&gt;The receiver type of the &lt;code&gt;block&lt;/code&gt; is whatever we pass as the first argument to the scope function: &lt;em&gt;with&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;We can access the receiver type &lt;code&gt;T&lt;/code&gt; from extension function &lt;code&gt;T.() -&amp;gt; R&lt;/code&gt; using the keyword: &lt;em&gt;this&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The function type parameter is the last parameter in the higher order function &lt;em&gt;with&lt;/em&gt;. That means, we can pass our function literal between curly braces as an argument after the &lt;em&gt;with&lt;/em&gt; function call parentheses.&lt;/li&gt;
&lt;li&gt;The return type of both the function type parameter and the scope function itself are same: &lt;code&gt;R&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The return type &lt;code&gt;R&lt;/code&gt; is generic. That means, whatever our function literal (that we pass as an argument to our &lt;em&gt;with&lt;/em&gt; function) returns, will be the return of our &lt;em&gt;with&lt;/em&gt; function.&lt;/li&gt;
&lt;li&gt;In function literals, the last statement gives return.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, using &lt;em&gt;with&lt;/em&gt;, we can perform multiple operations on an object without explicitly writing the name of the object or without taking another variable.&lt;/p&gt;

&lt;p&gt;However, If the variable is nullable, we cannot avoid that &lt;em&gt;this&lt;/em&gt; keyword in &lt;em&gt;with&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Suppose we were getting a nullable value in our last example from below function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getEvenOrNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;randomNumber&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="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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;randomNumber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rem&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="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="n"&gt;randomNumber&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Handling a nullable variable using &lt;em&gt;with&lt;/em&gt; would look like below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=zpQJ21irM&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;That doesn't look good! There must be a “nullability check” before we perform anything on a “nullable” variable.&lt;/p&gt;

&lt;p&gt;Using another scope function called &lt;em&gt;let&lt;/em&gt;, we can perform the “nullability check” and it also reduces the code than above and make it more pretty like below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=EVmOWSnQx&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So, let us examine: &lt;em&gt;let&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  let
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;let&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;T.let&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We know about an extension function. &lt;code&gt;T&lt;/code&gt; represents generics here. So, &lt;em&gt;let&lt;/em&gt; is a generic extension function. That means, we can use &lt;em&gt;let&lt;/em&gt; for any object. It is a member function for every class.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;T.let(block: (T) -&amp;gt; R): R&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Facts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; is a higher order function because it has a function type parameter.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; is also an extension function with a generic receiver type &lt;code&gt;T&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The receiver type &lt;code&gt;T&lt;/code&gt; is also a parameter of the function type &lt;code&gt;block&lt;/code&gt; here.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;block&lt;/code&gt; is our function type parameter of the &lt;code&gt;let&lt;/code&gt; function. We can pass a function type argument using function literal, lambda expression or an anonymous function. &lt;/li&gt;
&lt;li&gt;Because the &lt;code&gt;let&lt;/code&gt; function is generic and it has only one parameter which is also a generic, mostly we use function literal to pass as a function type argument.&lt;/li&gt;
&lt;li&gt;The function type parameter &lt;code&gt;block&lt;/code&gt; is the only parameter of a higher order function &lt;code&gt;let&lt;/code&gt; and the function type has also only one parameter &lt;code&gt;(T)&lt;/code&gt; which is also the receiver type. So, while calling &lt;code&gt;let&lt;/code&gt;, we can omit the function call parentheses, the parameter of the function type and the arrow &lt;code&gt;-&amp;gt;&lt;/code&gt;. We will just write our business logic between curly braces and we can access the parameter &lt;code&gt;(T)&lt;/code&gt; of the function type &lt;code&gt;block&lt;/code&gt;, using the keyword: &lt;code&gt;it&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;So, we access the receiver type as well as parameter of the function type inside the &lt;code&gt;let&lt;/code&gt; block using the keyword: &lt;code&gt;it&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The return type &lt;code&gt;R&lt;/code&gt; is generic. The &lt;code&gt;let&lt;/code&gt; function returns whatever our function literal (that we pass as a function type argument for the let function) returns.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why &lt;em&gt;let&lt;/em&gt;?
&lt;/h3&gt;

&lt;p&gt;Because, we don't like that &lt;em&gt;this?.&lt;/em&gt; for a "nullable variable" multiple times!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To provide “nullability check”.&lt;/li&gt;
&lt;li&gt;To introduce chain operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Wait. What? Chain operations? Let us understand what it is.&lt;/p&gt;

&lt;p&gt;Suppose we have a normal function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;doSomething&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="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;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name from the method doSomething is: $name"&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, let us do some multiple random operations like below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=2OfhQ0xBZ&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Now, let us do the same thing using &lt;em&gt;let&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=1-eyzXmno&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you can see, using &lt;em&gt;let&lt;/em&gt;, we could avoid new variables while using the return value from multiple chain operations.&lt;/p&gt;

&lt;p&gt;What?! Understanding the example:&lt;/p&gt;

&lt;p&gt;Let us see what we have done (how we have used the &lt;em&gt;let&lt;/em&gt; function):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;let&lt;/em&gt; is a generic extension function. So, any instance can call it. Here, the &lt;em&gt;let&lt;/em&gt; function is called on a nullable &lt;em&gt;int&lt;/em&gt; variable.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;let&lt;/em&gt; is also a higher order function. it has only one parameter. The parameter is of function type. The function type parameter has also only one parameter. Hence, we can omit the function call parentheses, the parameter of the function type and the arrow &lt;code&gt;-&amp;gt;&lt;/code&gt;. All we write is just our business logic between curly braces and we access the parameter of the function type (which is also the receiver type of our extension function) using the keyword &lt;em&gt;it&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;We have done the same. After the function name which is “&lt;em&gt;let&lt;/em&gt;”, there is our business logic between curly braces. The keyword &lt;em&gt;it&lt;/em&gt; represents our &lt;em&gt;int&lt;/em&gt; instance for the first &lt;em&gt;let block&lt;/em&gt; and a &lt;em&gt;StringBuilder&lt;/em&gt; instance for the second and third block.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Few things to note here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The last statement in the &lt;em&gt;let&lt;/em&gt; block decides the return type.&lt;/li&gt;
&lt;li&gt;We are returning the result and not the receiver type itself.&lt;/li&gt;
&lt;li&gt;We can check the nullability of the receiver type before we proceed.&lt;/li&gt;
&lt;li&gt;It is an extension function. So, we can chain multiple operations. The return type (last statement) of the previous &lt;em&gt;let block&lt;/em&gt; will become the receiver type of next function.&lt;/li&gt;
&lt;li&gt;When we want to access the receiver type, we can use the keyword &lt;em&gt;it&lt;/em&gt; (We have to use &lt;em&gt;it&lt;/em&gt; to access the receiver type).&lt;/li&gt;
&lt;li&gt;We can use any function on the receiver type from the &lt;em&gt;let block&lt;/em&gt; using the keyword: &lt;em&gt;it&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;We can pass the receiver type to another function within the let block using the keyword: &lt;em&gt;it&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  With Vs Let
&lt;/h2&gt;

&lt;p&gt;If there is a nullable value or chain operations, use &lt;em&gt;let&lt;/em&gt;. Otherwise, use &lt;em&gt;with&lt;/em&gt; because we can omit the keyword &lt;em&gt;this&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Can't we have a function that can omit the keyword &lt;em&gt;this&lt;/em&gt; like a &lt;em&gt;with&lt;/em&gt; function and support nullability check and chain operations like &lt;em&gt;let&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;Yes! Our next scope function &lt;em&gt;run&lt;/em&gt; is a hybrid product which has power of both &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;with&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Let us study: &lt;em&gt;run&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Run
&lt;/h2&gt;

&lt;p&gt;Below is the definition of the run function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;block&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;Similar to the &lt;em&gt;let&lt;/em&gt; function, &lt;em&gt;run&lt;/em&gt; is also an extension function and a higher order function. Hence, it also uses the keyword &lt;em&gt;inline&lt;/em&gt; to avoid the function object creation.&lt;/p&gt;

&lt;p&gt;The differences between &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;run&lt;/em&gt; functions are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The function type parameter&lt;/li&gt;
&lt;li&gt;Return type&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let us examine it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;T.run(block: T.() -&amp;gt; R): R&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The receiver type of an extension function and its function type is same: &lt;code&gt;T&lt;/code&gt;. That means, we can access the receiver type using the keyword: &lt;em&gt;this&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The keyword &lt;em&gt;this&lt;/em&gt; can be omitted when we want to call any function on it that reduces the code even more.&lt;/li&gt;
&lt;li&gt;It seems that the &lt;em&gt;run&lt;/em&gt; function has combined the power of &lt;em&gt;with&lt;/em&gt; and &lt;em&gt;let&lt;/em&gt;. Or maybe we can say it is a &lt;em&gt;with+&lt;/em&gt; version! &lt;em&gt;With&lt;/em&gt; was lacking the pros of an extension function (nullability check and chain operations) which the &lt;em&gt;run&lt;/em&gt; function satisfies.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of &lt;em&gt;let&lt;/em&gt;, If we use &lt;em&gt;run&lt;/em&gt; and rewrite the same function we have written earlier for &lt;em&gt;let&lt;/em&gt;, it would look like below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=_HjqodkY3&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;There are few things common between &lt;em&gt;run&lt;/em&gt; and &lt;em&gt;let&lt;/em&gt; as below. It will help us in deciding when to use them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Both are extension functions.&lt;/li&gt;
&lt;li&gt;Last statement is the return type.&lt;/li&gt;
&lt;li&gt;We can check the nullability before we proceed.&lt;/li&gt;
&lt;li&gt;Extension functions are helpful for chain operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, there is a small but important difference between them as below. It will help us in selecting the right scope function.&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;let&lt;/em&gt;, we access the receiver type using the keyword &lt;em&gt;it&lt;/em&gt; and it cannot be omitted. However, in &lt;em&gt;run&lt;/em&gt;, we access the receiver type using the keyword &lt;em&gt;this&lt;/em&gt; and it can be omitted sometimes.&lt;/p&gt;

&lt;p&gt;So, if we are not passing the receiver as an argument, we better use &lt;em&gt;run&lt;/em&gt; because it reduces the code. &lt;/p&gt;

&lt;p&gt;Considering above facts, we can rewrite our last example in a better way like below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=bWzUfvTb8&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;StringBuilder(toString())&lt;/code&gt; looks more concise than &lt;code&gt;StringBuilder(it.toString())&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;append(" test")&lt;/code&gt; looks more concise than &lt;code&gt;it.append(" test")&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, wouldn’t it be more concise if we can replace all &lt;em&gt;$this&lt;/em&gt; by &lt;em&gt;$it&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;But how? We have to use &lt;em&gt;this&lt;/em&gt; to refer to the context object in &lt;em&gt;run&lt;/em&gt;.&lt;br&gt;
Correct, but not for "&lt;em&gt;also&lt;/em&gt;”. We can use &lt;em&gt;it&lt;/em&gt; to refer to the context object while using &lt;em&gt;also&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then, how is it different from &lt;em&gt;let&lt;/em&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;also&lt;/em&gt;” returns the object itself whereas the last statement is “return type” for &lt;em&gt;let&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Ok, enough theory! Show me code!&lt;/p&gt;
&lt;h2&gt;
  
  
  Also
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;also&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
   &lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;&lt;code&gt;T.also&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is an extension function. That means, we can use it for “nullability check” before we proceed to work on a nullable value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;T&amp;gt; T.also(block: (T) -&amp;gt; Unit): T&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(block: (T) -&amp;gt; Unit)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;block&lt;/code&gt; is a function type parameter.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;block&lt;/code&gt; has one and only parameter &lt;code&gt;(T)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We know that if the function type has one and only argument, we can access the argument using the keyword &lt;em&gt;it&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;T&lt;/code&gt; , The only parameter of the &lt;code&gt;block&lt;/code&gt; is also a receiver type here.&lt;/p&gt;

&lt;p&gt;So, we will be accessing the receiver type &lt;code&gt;T&lt;/code&gt; using the keyword: &lt;em&gt;it&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The interesting part is, it returns the receiver type itself: &lt;code&gt;T&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;em&gt;also&lt;/em&gt;, We can rewrite our last example code as below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=cFW9SycFP&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As shown in the example, &lt;em&gt;also&lt;/em&gt; returns the object itself despite anything written in it's block and it uses &lt;em&gt;it&lt;/em&gt; to refer to the context object.&lt;/p&gt;

&lt;p&gt;So, It uses &lt;em&gt;it&lt;/em&gt; to refer to the context object. That means, if we need to call some methods on the context object, we will have to use &lt;em&gt;it&lt;/em&gt; like below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=4henvQOwa&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So, we had to use &lt;em&gt;it&lt;/em&gt; while calling a function on the context object from &lt;em&gt;also&lt;/em&gt; block similar to &lt;em&gt;let&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can we omit &lt;em&gt;it&lt;/em&gt; to make the code even more concise?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Yes! By using &lt;em&gt;apply&lt;/em&gt; instead of &lt;em&gt;also&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;Similar to &lt;em&gt;run&lt;/em&gt;, &lt;em&gt;apply&lt;/em&gt; also uses &lt;em&gt;this&lt;/em&gt; to refer to the context object. However, unlike &lt;em&gt;run&lt;/em&gt;, being a sibling of &lt;em&gt;also&lt;/em&gt;, &lt;em&gt;apply&lt;/em&gt; returns the context object itself.&lt;/p&gt;

&lt;p&gt;Let us examine &lt;em&gt;apply&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;.()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
   &lt;span class="nf"&gt;block&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;this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;T.apply&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is an extension function. Useful for “nullability check”.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;block: T.() -&amp;gt; Unit&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;block&lt;/code&gt; is a function type parameter.&lt;/p&gt;

&lt;p&gt;The receiver type of an extension function &lt;code&gt;T.apply&lt;/code&gt; and it's function type parameter &lt;code&gt;block&lt;/code&gt; is same: &lt;code&gt;T&lt;/code&gt;.&lt;br&gt;
And we know that we can refer to a receiver type using the keyword: &lt;em&gt;this&lt;/em&gt;&lt;br&gt;
And we also know that we can omit the keyword &lt;em&gt;this&lt;/em&gt; while calling a function on it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;T.apply(block: T.() -&amp;gt; Unit): T&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As we can see, it returns the receiver type itself.&lt;/p&gt;

&lt;p&gt;Let us see how we can include &lt;em&gt;apply&lt;/em&gt; in our last example:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=40U4Bx8MX&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion:
&lt;/h1&gt;

&lt;p&gt;We have scope functions to make the code more concise.&lt;br&gt;
All scope functions can be categorized or differentiated by two facts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The way to refer to the context object (this or it)&lt;/li&gt;
&lt;li&gt;Return type (the object itself or the last statement)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using these two facts, we can decide which scope function to use for a particular situation.&lt;/p&gt;

&lt;p&gt;1) If we need last statement as a return, we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;T.run&lt;/li&gt;
&lt;li&gt;T.let&lt;/li&gt;
&lt;li&gt;with&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anything that the &lt;em&gt;with&lt;/em&gt; can do, can be done by &lt;em&gt;T.run&lt;/em&gt; also. Clearly, &lt;em&gt;T.run&lt;/em&gt; wins here because of it's added “nullability check” and “chain operation” capabilities.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;T.run&lt;/em&gt; is better than &lt;em&gt;T.let&lt;/em&gt; if we are not passing the receiver type as an argument to another function.&lt;/p&gt;

&lt;p&gt;2) If we want to return the object itself, we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;apply (uses &lt;em&gt;this&lt;/em&gt; to refer to the object)&lt;/li&gt;
&lt;li&gt;also (uses &lt;em&gt;it&lt;/em&gt; to refer to the object)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Same as the comparison between &lt;em&gt;T.run&lt;/em&gt; Vs &lt;em&gt;T.let&lt;/em&gt;; &lt;em&gt;apply&lt;/em&gt; is better than &lt;em&gt;also&lt;/em&gt; if we are not passing the context object as an argument to another function.&lt;/p&gt;
&lt;h2&gt;
  
  
  Play an overview of this article:
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=kZJ0uXHK1&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;That’s all! If you find this article helpful, you can click on that heart icon 😉&lt;/p&gt;

&lt;p&gt;Applauds and creative critics are always welcome 😇. Your ❤ motivates me to write more articles.&lt;/p&gt;

&lt;p&gt;Thanks for reading the article! Have a great day 😇.&lt;/p&gt;

&lt;h1&gt;
  
  
  Relevant previous articles
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://dev.to/srdpatel/kotlin-inline-function-olf"&gt;Inline function&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/srdpatel/kotlin-extension-function-1dd7"&gt;Extension function and Receiver type&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd"&gt;Function type, Function literal, Lambda expression and Anonymous function&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can follow me to get the notification when I publish a new article.&lt;/p&gt;

&lt;h1&gt;
  
  
  Let us be Connected
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/srdpatel"&gt;https://www.linkedin.com/in/srdpatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/iSrdPatel"&gt;https://twitter.com/iSrdPatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tags: kotlin, scopeFunctions, android&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>scopefunction</category>
      <category>android</category>
      <category>functiontype</category>
    </item>
    <item>
      <title>Kotlin: Inline function</title>
      <dc:creator>Sagar</dc:creator>
      <pubDate>Mon, 04 May 2020 10:41:51 +0000</pubDate>
      <link>https://dev.to/srdpatel/kotlin-inline-function-olf</link>
      <guid>https://dev.to/srdpatel/kotlin-inline-function-olf</guid>
      <description>&lt;h1&gt;
  
  
  Prerequisite/Previous articles:
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd"&gt;Kotlin: Function type, higher order function, function literal, lambda expression and an anonymous function&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/srdpatel/kotlin-extension-function-1dd7"&gt;Kotlin: Extension function and Receiver type&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Inline function
&lt;/h1&gt;

&lt;p&gt;We &lt;a href="https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd"&gt;know&lt;/a&gt; that a new function object is created for each and every function type in a higher order function. It causes memory allocation and hence, runtime overhead. To avoid this or say, to improve the performance, we can use the keyword &lt;strong&gt;inline&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However, that doesn’t mean we should inline each and every function! In fact, when we try to &lt;em&gt;inline&lt;/em&gt; a function that is not accepting a function type, the IDE will tell us to remove the &lt;em&gt;inline&lt;/em&gt; keyword.&lt;/p&gt;

&lt;p&gt;Also, we should not &lt;em&gt;inline&lt;/em&gt; any higher order function that has more than 3 lines because generated code grows a lot for an &lt;em&gt;inline function&lt;/em&gt;. You can check the Kotlin standard library to get an idea about when to use inline functions.&lt;/p&gt;

&lt;p&gt;When using an inline function, we are not allowed to pass a function type parameter to another function. However, if it is necessary, we can mark such a function type as a &lt;strong&gt;noinline&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let us understand it by an example:&lt;/p&gt;

&lt;p&gt;Suppose we have our higher order function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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="n"&gt;ftOne&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ftTwo&lt;/span&gt;&lt;span class="p"&gt;:&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;-&amp;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="cm"&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, in such case, in order to prevent new function object creation, we can make the function inline as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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="n"&gt;ftOne&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ftTwo&lt;/span&gt;&lt;span class="p"&gt;:&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;-&amp;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="cm"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, if we are passing any function type to another function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Compile time error… Illegal usage of inline function type ftOne...&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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="n"&gt;ftOne&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ftTwo&lt;/span&gt;&lt;span class="p"&gt;:&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;-&amp;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="c1"&gt;//passing a function type to another function&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;funOne&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;someFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ftOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="cm"&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, the compiler will complain to us: Illegal usage of inline function type ftOne…&lt;/p&gt;

&lt;p&gt;To solve that, we can rewrite our function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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="k"&gt;noinline&lt;/span&gt; &lt;span class="n"&gt;ftOne&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ftTwo&lt;/span&gt;&lt;span class="p"&gt;:&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;-&amp;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="c1"&gt;//passing a function type to another function&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;funOne&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;someFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ftOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, if there is only one lambda parameter and we are passing it to another function, it doesn’t make any sense to make such a higher order function inline and the compiler will also suggest the same!&lt;/p&gt;

&lt;p&gt;Let us understand it by an example:&lt;/p&gt;

&lt;p&gt;Suppose we have a higher order function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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="k"&gt;noinline&lt;/span&gt; &lt;span class="n"&gt;ftOne&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;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="c1"&gt;//passing a function type to another function&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;funOne&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;someFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ftOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="cm"&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, the compiler will tell us to not use the inline keyword when there is only one lambda parameter and we are passing it to another function. So, we can rewrite above function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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="n"&gt;ftOne&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;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="c1"&gt;//passing a function type to another function&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;funOne&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;someFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ftOne&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that we had to remove the keyword &lt;em&gt;noinline&lt;/em&gt; as well because it can be used only for inline functions!&lt;/p&gt;

&lt;p&gt;That’s all!&lt;/p&gt;

&lt;p&gt;If you have read this article, if this article has helped you, you can click on that ❤ icon. Your ❤ motivates me to write more articles.&lt;/p&gt;

&lt;p&gt;You can follow me to get the notification when I publish a new article. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let us be Connected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/srdpatel"&gt;https://www.linkedin.com/in/srdpatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/iSrdPatel"&gt;https://twitter.com/iSrdPatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tags: kotlin, inline&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>inline</category>
    </item>
    <item>
      <title>Kotlin: Extension function and Receiver type</title>
      <dc:creator>Sagar</dc:creator>
      <pubDate>Sat, 02 May 2020 01:01:11 +0000</pubDate>
      <link>https://dev.to/srdpatel/kotlin-extension-function-1dd7</link>
      <guid>https://dev.to/srdpatel/kotlin-extension-function-1dd7</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;Prerequisite/Previous Part&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd"&gt;Kotlin: Function type, function literal, lambda expression and anonymous function&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Extension Function
&lt;/h1&gt;

&lt;p&gt;General scenario: We make a class. We write a few methods. Later, we realize that we need more methods for the class. We write more methods in the same class.&lt;/p&gt;

&lt;p&gt;But, if the class is in the third-party library that we are using through dependency or in such a way that we cannot add new functions in such a class, we cannot modify that class.&lt;/p&gt;

&lt;p&gt;In kotlin, &lt;strong&gt;we can add a new function for any class in any other class too&lt;/strong&gt;. Such a function is called an “Extension function” and we can &lt;strong&gt;use&lt;/strong&gt; such an extension function only &lt;strong&gt;inside the class where it is created&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So now, we can add new functions even for the class that is in the third-party library. Note that it is not limited to third-party library class only! Main thing is, &lt;em&gt;we can add a new function for any class in any other class too&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;

&lt;p&gt;In order to create an extension function, we write the class name (also known as a receiver type) followed by a dot, followed by our function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Here, OurClassName is called a receiver type&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;OurClassName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ourExtensionFunction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt; * Extension function. Here, String is known as receiver type. &lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt; * That means, we can call this function on any String. &lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt; * The extension function can be called only from the   &lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt; * class where it is defined.&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt; */&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extensionFun&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//We can access the receiver (Here, it is String) using "this" keyword inside the //business logic&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;"from extension function: $this"&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;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We can access an instance of the &lt;strong&gt;receiver type using &lt;em&gt;this&lt;/em&gt; keyword inside the business logic&lt;/strong&gt; as shown above.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to call / use an extension function?
&lt;/h3&gt;



&lt;div class="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;myString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"MyString"&lt;/span&gt;
&lt;span class="n"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extensionFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;//prints: from extension function: MyString&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h1&gt;
  
  
  Receiver
&lt;/h1&gt;

&lt;p&gt;We have seen a receiver type in an extension function. Similarly, a function type, function literal, lambda expression and an anonymous function can also have a receiver type.&lt;/p&gt;

&lt;h3&gt;
  
  
  A function type with receiver
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Syntax/Signature of a function type with receiver:
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;ft&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="c1"&gt;// Here, the receiver type is: Int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Function type with the receiver as a parameter
&lt;/h3&gt;

&lt;p&gt;We can have a higher order function having a function type parameter with receiver as below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//A higher order function whose function type parameter has a receiver&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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="n"&gt;y&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="n"&gt;ft&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;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="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;ftWithReceiver&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//OR ft(x, y) where x is a receiver.&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;"doSomething: $ftWithReceiver"&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;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Note that &lt;strong&gt;when a receiver is treated as an argument, it must be a first argument&lt;/strong&gt; as shown in the comment of the above function.&lt;/p&gt;

&lt;p&gt;In order to call above higher order function, we can do one of the below things:&lt;/p&gt;

&lt;h3&gt;
  
  
  Function literal with receiver
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//A function literal, accessing the receiver by using the keyword: this&lt;/span&gt;
&lt;span class="nf"&gt;doSomething&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;//prints: doSomething: -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let us understand our “function literal”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Our function literal has been passed as an argument against:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ft: Int.(Int) -&amp;gt; Int)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The function type parameter is expecting an Int parameter. Our function literal has also an Int parameter:&lt;br&gt;
&lt;br&gt;
&lt;code&gt;y: Int&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the business logic, we have used the keyword: &lt;em&gt;this&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
&lt;code&gt;-&amp;gt; this - y&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We access an instance of our receiver in a business logic of an extension function through the keyword: &lt;em&gt;this&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Our &lt;em&gt;this&lt;/em&gt; keyword has been inferred as a receiver type: &lt;em&gt;Int&lt;/em&gt; for:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;ft: Int.(Int) -&amp;gt; Int)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our &lt;em&gt;this&lt;/em&gt; keyword refers to a receiver type: &lt;em&gt;Int&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Lambda expression with receiver
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//A lambda with receiver type. Accessing the receiver by using the keyword: this&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;ftSubtraction&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;And then we can use that lambda to pass as an argument to our higher order function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//passing a lambda for the higher order function&lt;/span&gt;
&lt;span class="nf"&gt;doSomething&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="n"&gt;ftSubtraction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//prints: doSomething: -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Same receiver for both an extension function and a function type
&lt;/h3&gt;

&lt;p&gt;Suppose we have a higher order function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Same receiver type for both an extension function and its function type param&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&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="n"&gt;ft&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;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="c1"&gt;//In such case, we can omit the receiver while calling the function type&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;ftWithReceiver&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//It is Int.ft(y) &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;"doSomething: $ftWithReceiver"&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;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;So, as shown in the example above: &lt;strong&gt;When the receiver type of an extension function is the same as its function type parameter, we can omit the keyword &lt;em&gt;this&lt;/em&gt; while calling any function on it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can call above extension function like below:&lt;/p&gt;

&lt;h5&gt;
  
  
  Using Anonymous function
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//passing an anonymous function&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&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="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;//prints: doSomething: -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h5&gt;
  
  
  Using lambda
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//passing a lambda for the higher order function&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doSomething&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="n"&gt;ftSubtraction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//prints: doSomething: -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;You can play the concept below:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://play.kotlinlang.org/embed?short=OKsyHRgjy&amp;amp;from&amp;amp;to&amp;amp;theme&amp;amp;readOnly"&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;That's all!&lt;/p&gt;

&lt;p&gt;If you have read this article, if this article has helped you, you can give me your ❤ 😉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd"&gt;Link to previous article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/srdpatel/kotlin-inline-function-olf"&gt;Next article: inline&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let us be Connected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/srdpatel"&gt;https://www.linkedin.com/in/srdpatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/iSrdPatel"&gt;https://twitter.com/iSrdPatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tags: kotlin, function type, function literal, lambda expression, anonymous function, higher order function, extension function, receiver type&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>extensionfunction</category>
      <category>receiver</category>
    </item>
    <item>
      <title>Kotlin: Function type, Function literal, Lambda expression and Anonymous function</title>
      <dc:creator>Sagar</dc:creator>
      <pubDate>Fri, 01 May 2020 01:15:43 +0000</pubDate>
      <link>https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd</link>
      <guid>https://dev.to/srdpatel/kotlin-function-type-function-literal-lambda-expression-and-anonymous-function-lbd</guid>
      <description>&lt;h1&gt;
  
  
  Function Type
&lt;/h1&gt;

&lt;p&gt;We know data types. Similarly, we have function type too in kotlin.&lt;/p&gt;

&lt;h3&gt;
  
  
  Signature / Syntax
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Name: (only comma separated data types) -&amp;gt; return data type

    ft: (Int, Int) -&amp;gt; Int
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  How to call / execute a function type?
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="nf"&gt;ft&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="c1"&gt;//OR&lt;/span&gt;

    &lt;span class="n"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Usage:
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Function type as an interface
&lt;/h4&gt;

&lt;p&gt;Like an interface, we can implement a function type in a kotlin class. When we implement a function type, we get a method called “invoke” to override having a similar signature of the implemented function type.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//Implementing a function type like an interface&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClassName&lt;/span&gt; &lt;span class="p"&gt;:&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="cm"&gt;/**We get a method "invoke" to override with similar signature of     &lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt;     * function type we implemented&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&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;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p1&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="n"&gt;p2&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="nc"&gt;Int&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;p1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;p2&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;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h4&gt;
  
  
  Function type as a parameter
&lt;/h4&gt;

&lt;p&gt;As a function parameter in a higher order function. The function that takes one or more function types as a parameter/s or/and the function that returns a function type is known as a higher order function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;doSomething&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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="n"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&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;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;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ft&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;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//OR ft.invoke(a, b)&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="n"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;”&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;There is one thing to remember here:.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A new function object will be created for each and every function type of a higher order function.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  How to call/use/execute such a higher order function?
&lt;/h4&gt;

&lt;p&gt;We know how to call a function but we may not know how to pass a function type argument/s in any higher order function!&lt;/p&gt;

&lt;p&gt;We can pass function type arguments using function literal, lambda expression or by anonymous function. Let us check these all one by one.&lt;/p&gt;

&lt;h1&gt;
  
  
  Function literal
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Signature / Syntax: 1
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    { comma separated pascal parameters -&amp;gt; business logic }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&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="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Note that the last statement in a function literal is considered as a return statement. So, the last statement in a function literal decides the return type.&lt;/p&gt;

&lt;p&gt;We can use function literal to pass as a function type argument for higher order function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//Calling a higher order function &lt;/span&gt;

    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;doSomething&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="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x&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="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt; 

    &lt;span class="c1"&gt;//prints: doSomething: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;OR if the function type parameter is the last parameter in the higher order function, we can write our function literal after the closing function parenthesis as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//Calling a higher order function&lt;/span&gt;

    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;doSomething&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="n"&gt;x&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="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;//prints: doSomething: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;OR if the higher order function has only one parameter or say if the function type parameter is one and only parameter in a higher order function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;   &lt;span class="c1"&gt;//region A higher order function that has one and only function type parameter&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;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="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;ft&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="cm"&gt;/*...*/&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="c1"&gt;//endregion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;While calling such a higher order function, we can omit the function call parentheses like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;        &lt;span class="nf"&gt;doSomething&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&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="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Syntax / Signature 2
&lt;/h3&gt;

&lt;p&gt;Also, if the function type is only parameter in a higher order function and if the function type has also only one parameter like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;:&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;-&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="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;While passing such a function literal, in addition to function call parentheses, we can also omit the parameter and an arrow &lt;code&gt;-&amp;gt;&lt;/code&gt; ! We just write core business logic between curly braces and we can access our single parameter through the keyword it like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;doSomething&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"$it"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;And if the higher order function has only one function type parameter and the function type has no parameter like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doSomethingY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ft&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&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="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/*...*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We can simply write our business logic between curly braces right after the higher order function name as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;doSomethingY&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"test"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Types:
&lt;/h3&gt;

&lt;p&gt;There are two types of function literals.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Lambda expression&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Anonymous function&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Lambda expression
&lt;/h1&gt;

&lt;p&gt;Lambda expression is just another way to define a function in a short way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Signature / Syntax 1
&lt;/h3&gt;

&lt;p&gt;We can either specify explicit function type and let the data type of parameters inferred in below format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;nameOfTheLambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;explicit&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;type&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="n"&gt;comma&lt;/span&gt; &lt;span class="n"&gt;separated&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Inferred&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;business&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or we can give explicit type annotation to parameters and let the function type inferred like below format:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;nameOfTheLambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inferred&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;type&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="n"&gt;comma&lt;/span&gt; &lt;span class="n"&gt;separated&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Explicit&lt;/span&gt; &lt;span class="n"&gt;dataType&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;business&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Or we can write both function type and data type of parameter (but why?) like below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;nameOfTheLambda&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;explicit&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="n"&gt;type&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="n"&gt;comma&lt;/span&gt; &lt;span class="n"&gt;separated&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Explicit&lt;/span&gt; &lt;span class="n"&gt;dataType&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;business&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Let us see an example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="cm"&gt;/**&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt;     * nameOfTheLambda: function type &lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt;     * = { comma separated pascal parameters -&amp;gt; business logic }&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="cm"&gt;     */&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&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="n"&gt;y&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Signature / Syntax 2 for special case
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;If a lambda has only one parameter, we can omit everything except a business logic inside the curly braces and we can access the single argument using the keyword: &lt;em&gt;it&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Suppose we have a lambda expression like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;lambdaIncrement&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&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;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We can write the equivalent lambda as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;lambdaIncrement&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Summary for a lambda expression having a single parameter&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Usage: How to call / execute / use lambda expression
&lt;/h3&gt;

&lt;p&gt;We can use above lambda expression directly like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lambda&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="c1"&gt;//prints: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;OR we can pass a lambda as a function type argument to a higher order function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;doSomething&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="n"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;//prints: doSomething: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Sometimes, the common term “&lt;em&gt;lambda&lt;/em&gt;” is used to represent a function type, function literal or a lambda expression.&lt;/p&gt;

&lt;h1&gt;
  
  
  Anonymous function
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Signature / Syntax
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//fun(comma separated pascal parameters) = business logic&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We can store an anonymous function in a variable like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;anonymousFunction&lt;/span&gt;&lt;span class="p"&gt;:&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="nc"&gt;Int&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="k"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Usage: How to use an anonymous function
&lt;/h3&gt;

&lt;p&gt;We use an anonymous function as an argument for a higher order function like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;doSomething&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="k"&gt;fun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;))&lt;/span&gt; 

    &lt;span class="c1"&gt;//prints: doSomething: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We can use an anonymous function through a variable to pass as an argument in a higher order function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;doSomething&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="n"&gt;anonymouseFunction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

    &lt;span class="c1"&gt;//prints: doSomething: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;That’s all! If you have read this article, if you find this article helpful, you can click on that heart icon 😉&lt;/p&gt;

&lt;p&gt;Applauds and creative critics are always welcome 😇&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/srdpatel/kotlin-extension-function-1dd7"&gt;next article&lt;/a&gt;, we will learn about &lt;strong&gt;Extension function and Receiver type&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading the article! Have a great day 😇&lt;/p&gt;

&lt;h1&gt;
  
  
  Let us be Connected
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/srdpatel"&gt;https://www.linkedin.com/in/srdpatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/iSrdPatel"&gt;https://twitter.com/iSrdPatel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tags: kotlin, function type, function literal, lambda expression, anonymous function, higher order function&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>functiontype</category>
      <category>functionliteral</category>
      <category>lambdaexpression</category>
    </item>
  </channel>
</rss>
