ForEach inside for everyone in Kotlin

In Kotlin, if you want to use an element forEach, you can use the keyword it. So now I'm wondering what should I do if I have forEachinside forEach:

list.forEach {
    val parent = it
    it.forEach {
        // `it` now become the element of the parent.
    }
}

I think that defining a new variable just for naming convention would be so stupid. Is there any other solution for this problem?

+4
source share
2 answers

it- This is only the default parameter name in all private arguments. You can specify the parameter name yourself:

collection.forEach { customName -> 
    ...
}
+23
source

Artyom, , , .

:

>>  var a = "abc"
>>  a?.let { it[2]?.let { it } }
c

- "". "it", , "it [2]", "c" "abc".

, "", - , . , "It" "let" let, .

>>  var a = "abc"
>>  a?.let { outerIt -> outerIt[2]?.let { it } }
c

, , . :

>>  var a = "abc"
>>  a?.let { outerIt -> outerIt[2]?.let { "${outerIt[1]} $it" } }
b c

"", , .

+2
source

Source: https://habr.com/ru/post/1681840/


All Articles