Home > other >  kotlin add method, having difficulty understanding the "it" arguement
kotlin add method, having difficulty understanding the "it" arguement

Time:05-09

private fun defaultOptions() {
   val options = ArrayList<TextView>() 
   tvoptionone?.let { options.add(0, it) } 
}

I am currently using the add(index, element) method in kotlin, However, I don't seem to understand what it represents in the element parameter of the add method.

These are the parameters for the add method I am trying to use

add(index, element)

CodePudding user response:

In the below code

tvoptionone?.let { options.add(0, it) } }

it refers to tvoptionone
notice that lambda passed to let will be called only when tvoptionone is not null, so here it refers to tvoptionone and its value is not null

CodePudding user response:

Questionmark after variable tvoptionone indicates that this variable can be null. If you write just:

options.add(0, tvoptionone)

and variable happens to be null then the add method will throw an error with wording like param element cannot be null or so.

Keyword let, in this particular example, is kind of a guardian against passing null into add method. If tvoptionone has some value (is not null) then it will be tvoptionone itself. Otherwise add method will not be called at all and compilation error will be avoided.

CodePudding user response:

it is the context object on which you've used the let function.

As you've used it with safe call operator (?.) it would only call let if object is non null.

Using ?.let ensures the lambda to be executed only when the object is non null. ?. ensures that object has to be non null and let makes that object available as it inside the lamda.

Here

tvoptionone?.let { options.add(0, it) } 

it is a TextView as tvoptionone is a TextView, and it has a value same as tvoptionone.

CodePudding user response:

Scope functions are the ones you run on a value, and provide a function to run using that value. There's two kinds of scope functions in Kotlin - the ones where the value is passed in as a parameter (let, also etc.) and the ones where it becomes this (run, apply etc). They work the same, it just changes how you interact with the value - sometimes one is more convenient or suitable than the other

it is just the default name for the parameter passed in:

// these two are the same thing
name.let { println("Hi $it") }
name.let { it -> println("Hi $it") }
// rename it to something that reads better if you like
personData.firstName.let { name -> println("Hi $name") }

People have mentioned the null-check feature, where you can make the let block only run if the value is non-null:

name?.let { println("Hi $it, looking very not-null today") }

but another use for it is creating a temporary variable. If you have a var, it's possible the value will change while you're using it, so it's common to take a copy of it (so you know your copy won't change):

var importantNumber = 123
if (importantNumber > 100) {
    // but it might have just been changed by another thread / coroutine and be < 100!
    doThing(importantNumber)
}
var importantNumber = 123
val copy = importantNumber
if (copy > 100) {
    // the copy can't change, so we know once we've checked it, it's fine
    doThing(copy)
}

since let creates a variable to pass in as a parameter, it's basically making a copy in the same way:

var importantNumber = 123
importantNumber.let { if (it > 100) doThing(it) } // safe because 'it' won't change

and that's super important for nullable vars - if you check they're null, you need to know they'll stay null. Using ?.let guarantees that the value passed in as it will be non-null, because it's a copy that's been null-checked

  • Related