oday
06/30/2022, 9:34 AMlist.addAll(item1, item2, if (flag) item3))
something along those lines, I know it isnt the way to do it but is there an actual wayribesg
06/30/2022, 9:39 AMlist.addAll(listOfNotNull(item1, item2, if (flag) item3 else null))
But don’t do that.Joffrey
06/30/2022, 9:39 AMList
? Because in the snippet you showed it seems you have several variables but not a list to add.
Also, why are you using a mutable list in the first place here? Are you interacting with the mutable list long term? Or is it a temporary object that is then just used as a List
? If the latter, buildList { ... }
would likely be more appropriate.Youssef Shoaib [MOD]
06/30/2022, 9:42 AMtakeIf
list.addAll(listOfNotNull(..., item3.takeIf { flag }))
But probably the recommended, and cheaper way to do this is with `buildList`:
buildList(3) { // 3 is the maximum size of things you're going to add
add(item1)
add(item2)
if(flag) add(item3)
}
oday
06/30/2022, 9:43 AM