Ellen Spertus
02/17/2021, 9:26 PMboxes.forEach {
it.setOnClickListener {
makeColored(it)
}
}
2️⃣
boxes.forEach { it.setOnClickListener { makeColored(it) } }
3️⃣ @Marc Knaup
boxes.forEach { it.setOnClickListener(::makeColored) }
4️⃣ @ephemient
for (box in boxes) {
box.setOnClickListener { makeColored(it) }
}
5️⃣ @Marc Knaup @ephemient
for (box in boxes) {
box.setOnClickListener(::makeColored)
}
Marc Knaup
02/17/2021, 9:30 PMboxes.forEach { it.setOnClickListener(::makeColored) }
if possible 😁
In your cases 1 & 2 you’ll get a warning that you use two it
. One parameter should be named.
I’d use 2) if fits with line limit, 1) otherwise.ephemient
02/17/2021, 9:41 PM.forEach()
if a for ()
loop would work. if you have a nullable chain then sure, ?.forEach()
is nice, but otherwise there's no reason.
for (box in boxes) {
box.setOnClickListener { makeColored(it) }
}
Ellen Spertus
02/17/2021, 9:43 PMEllen Spertus
02/17/2021, 9:43 PMMarc Knaup
02/17/2021, 9:44 PMfor (box in boxes)
box.setOnClickListener(::makeColored)
Marc Knaup
02/17/2021, 9:45 PMforEach
unless it’s part of a longer chain or necessary for some other reason.Ellen Spertus
02/17/2021, 9:45 PMMarc Knaup
02/17/2021, 9:45 PMEllen Spertus
02/17/2021, 9:47 PMfor (box in boxes) box.setOnClickListener(::makeColored))
and satisfy the coding guidelines as well as the compiler.therealbluepandabear
03/24/2021, 10:55 PMgildor
05/25/2021, 10:04 AMboxes.forEach { box ->
gildor
05/25/2021, 10:05 AM