Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: difference between IT and THIS keyword

Tags:

android

kotlin

In one of the kotlin interviews, someone asked me the difference between it & this keywords.

I have a search on google but unable to find a proper answer for the question.

Can someone guide me what's the actual difference between these two?

I know this is very basic question, I am a novice in the kotlin.

like image 407
Harsh Shah Avatar asked Mar 02 '20 08:03

Harsh Shah


People also ask

What is this () in Kotlin?

In Kotlin, the “this” keyword allows us to refer to the instance of a class whose function we happen to be running.

What is the use of :: in Kotlin?

:: converts a Kotlin function into a lambda.

What is the difference between * and any in Kotlin generics?

When we define a collection with "*", it should contain the object of only that type. There should not be any mix and match between the data types inside a collection. If we use "Any", we can mix and match the data types, which means we can have multiple data types in a collection.

What does != Mean in Kotlin?

The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.


3 Answers

You need to know about Scope Functions:

The Kotlin standard library contains several functions whose sole purpose is to execute a block of code within the context of an object. When you call such a function on an object with a lambda expression provided, it forms a temporary scope.

Inside this scope there is a Context object either as this or it

In Scope functions run, apply and with the scope is (temporarily) changed to the scope of the object you are calling this function on:

val str = "Hello"
str.run {
    //Here this refers to str
}

In Scope functions let, also the scope is not changed (remains the same as caller scope) but your lambda will receive the context as it inside the lambda:

val str = "Hello"
str.let {
    //Here it refers to str
}

You can check the links for more information.

like image 181
Saeed Entezari Avatar answered Oct 09 '22 10:10

Saeed Entezari


it is only relevant inside a lambda with a single parameter. It is the default name for a single parameter and is a shorthand that allows you to omit naming the single parameter. A function that is declared this way might look like this:

(String) -> Unit

In a lambda, this is the receiver argument. It only works if the function is defined as having a receiver, like this:

String.() -> Unit

If the function declaration does not have a receiver, this has the same meaning it does outside the scope of the lambda. For an extension function, that’s the receiver of the extension function. Otherwise, it’s the class containing the function.

like image 31
Tenfour04 Avatar answered Oct 09 '22 09:10

Tenfour04


Difference between it & this keywords can be explained by taking example of lambda method receivers (a.k.a higher order functions).

Let's say you've written a function or using a function which provides you callback as lambda method receiver. Something like this: () -> Unit

So, there are two possibilities how you want your callback to be:

  1. Providing parameter to callback

    • Parameter by callback means you want to give your callback a parameter that caller can use on the time of invocation, also considered as it.

Whatever written above simply means: (Int) -> Unit. this functional method parameter can give you integer at the time of invocation.

Check out the snippet below:

fun someMethodWithCallback(callback: (Int) -> Unit) {
    callback(0)
}

// On the time of consumption, the `Int` parameter by default exposed to callback as it parameter.
obj.someMethodWithCallback { it -> // Here it is the method parameter of callback that we passed, you can also rename it to any other named value
    // it can be directly used as Int value if needed or you can rename it at receiver above
}

Note: You can provide multiple parameters to callback and then you won't be able to receive it, rather callback would provide you number of variables passed instead.

  1. Providing object to callback

    • Another way to provide callback is by providing object itself as callback parameter. Which means that callback syntax slightly changes and gives you object itself as callback parameter this.

Whatever written above simply means: Int.() -> Unit. this functional method object can give you integer at the time of invocation.

Check out the snippet below:

fun someMethodWithCallback(callback: Int.() -> Unit) {
    callback(0)
}

// On the time of consumption, the `Int` parameter by default exposed to callback as it parameter.
obj.someMethodWithCallback { this // Here this is the method object of callback that we passed, you can not rename it to anything else
    // it can be used as Int value by referencing as this
}

Hope it make sense!

like image 5
Jeel Vankhede Avatar answered Oct 09 '22 10:10

Jeel Vankhede