Raj
Raj

Reputation: 4079

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?

Upvotes: 17

Views: 7743

Answers (1)

glisu
glisu

Reputation: 1126

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

Upvotes: 28

Related Questions