Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implicit parameter of enclosing lambda is shadowed

val listPlans: List<Plan> = newPlans.mapTry {
            it.data.map {
                Plan(it.id, it.name, it.phone, it.desc, it.email)
            }.toList()
        }

Kotlin newbie writing code and IntelliJ highglights it in the Plan(it.id, it.name, it.phone, it.desc, it.email) and says that implicit parameter of enclosing lambda is shadowed.

Is it ok to leave this as it is? If not, what can I replace it with?

like image 261
Raj Avatar asked Apr 17 '19 20:04

Raj


1 Answers

You have nested lambdas, so "it" will represent the implicit parameter of the innermost lambda. This warning is mainly for readability purposes and avoiding confusion because the meaning of "it" gets more unclear as you nest more lambdas. I recommend you to specify lambda parameters explicitly for nested lambdas:

val listPlans: List<Plan> = newPlans.mapTry { plan ->
        plan.data.map {
            Plan(it.id, it.name, it.phone, it.desc, it.email)
        }.toList()
    }

or

val listPlans: List<Plan> = newPlans.mapTry {
        it.data.map { planData ->
            Plan(planData.id, planData.name, planData.phone, planData.desc, planData.email)
        }.toList()
    }

or both:

    val listPlans: List<Plan> = newPlans.mapTry { plan ->
        plan.data.map { planData ->
            Plan(planData.id, planData.name, planData.phone, planData.desc, planData.email)
        }.toList()
    }

It is worth to point out that this is one of the reasons why JetBrains team is planning to remove "it" functionality

like image 83
glisu Avatar answered Dec 27 '22 00:12

glisu