Kotlin: Extension function and Receiver type

Sagar Patel
3 min readMay 2, 2020

Prerequisite / Previous Article

[Kotlin: Function type, Function literal, Lambda expression, and Anonymous function](https://link.medium.com/fEkk6DpZW5)

Kotlin: Function type, function literal, lambda expression, and An anonymous function

Extension function

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.

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.

In kotlin, we can add a new function for any class in any other class too. Such a function is called an “Extension function,” and we can use it only inside the class where we create it.

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 classes only! The main thing is we can add a new function for any class in any other class without inheritance.

Syntax

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:

//Here, OurClassName is called a receiver type

fun OurClassName.ourExtensionFunction

For example:

We can access an instance of the receiver type using this keyword inside the business logic, as shown above.

How to call/use an extension function?

Receiver

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.

A function type with receiver

Syntax/Signature of a function type with receiver:

ft: Int.(Int) -> Int // Here, the receiver type is: Int

Function type with the receiver as a parameter

We can have a higher-order function having a function type parameter with a receiver as below:

Note that when a receiver is treated as an argument, it must be the first argument, as shown in the comment of the above function.

In order to call the above higher-order function, we can use one of the below things:

Function literal with receiver

Let us understand our “function literal.”

{ y: Int -> this - y }
  • We have passed our function literal as an argument against:

ft: Int.(Int) -> Int)

  • The function type parameter is expecting an Int parameter. Our function literal has also an Int parameter:

y: Int

  • Inside the business logic, we have used the keyword: this

-> this - y

  • We access an instance of our receiver in a business logic of an extension function through the keyword: this
  • Our this keyword has been inferred as a receiver type: Int for:
ft: Int.(Int) -> Int
  • Our this keyword refers to a receiver type: Int

Lambda expression with receiver

And then, we can use that lambda to pass as an argument to our higher-order function as below:

Same receiver for both an extension function and a function type

Suppose we have a higher-order function like below:

So, as shown in the example above: When the receiver type of an extension function is the same as its function type parameter, we can omit the keyword “this” while calling any function on it.

We can call the above extension function like below:

Using Anonymous function:

Using lambda:

You can play the concept below:

That’s all!

If you have read this article, if this article has helped you, you can click and hold that 👏 clap icon for a few seconds 😉

Link to the previous article

Next article: Kotlin: Inline function

You can follow me here on medium or on various social networks to get notifications when I publish a new article or discuss android development, soft skill development, or multiple ideas and hacks. So let us become better together.

Let us learn together from each other’s experiences.

Let us be Connected

https://www.linkedin.com/in/srdpatel/

https://twitter.com/LogicalSagar

Tags: kotlin, function type, function literal, lambda expression, an anonymous function, higher-order function, extension function, receiver type

I published the original article at https://dev.to on May 2, 2020.

--

--