Kotlin `it` in nested lambdas: our AI review keeps...
# getting-started
p
Kotlin
it
in nested lambdas: our AI review keeps flagging this, and I think the rule is too strict. The official conventions (https://kotlinlang.org/docs/coding-conventions.html#lambda-parameters), which our guidelines adopt verbatim, say: “In nested lambdas with parameters, always declare parameters explicitly.” So the reviewer flags code like this:
Copy code
labels.any { label -> wantedLabels.any { it in label.name } }
and wants this instead:
Copy code
labels.any { label -> wantedLabels.any { keyword -> keyword in label.name } }
But
it
isn’t ambiguous here: the outer lambda names its parameter, so
it
can only mean one thing. The extra name adds nothing. Proposal: add an exception to our KOTLIN_CODING_GUIDELINES.md: a nested lambda may use
it
as long as no enclosing lambda uses
it
. So this is still not OK:
Copy code
orders.filter { it.items.any { it.price > limit } }
Objections?
c
This looks ok to me:
Copy code
labels.any { label -> wantedLabels.any { it in label.name } }
This is too verbose I think:
Copy code
labels.any { label -> wantedLabels.any { keyword -> keyword in label.name } }
This is too confusing I think:
Copy code
orders.filter { it.items.any { it.price > limit } }
➕ 4
p
Yes, I agree. Do you think it worth a proposal (PR) to refine the lambda parameters section?
c
You can send one, but I’m not sure it’s very likely to be merged, they are very careful to avoid changes in the guide.