Hey, is it possible for listOf() function somethin...
# getting-started
m
Hey, is it possible for listOf() function something like that:
Copy code
listOf(
     someString1 if someString1 is not null,
     someString2,
     someString3 if someString3 is digits only
)
?
j
actually that's not possible. you can do inline if statement but a return is needed for
listOf()
it would be value or null.
Copy code
listOf(
    	if(name.contains("J")) name else null,
        if(name.contains("c")) name else null,
    ).filterNotNull()
m
OK, thanks
1
g
listOfNotNull(...)
is a bit shorter 🙂
👆 1
m
How that function works? It automatically remove null elements?
g
Yeah. It's the same as
listOf(...).filterNotNull()
.
🙌 1
m
@Gleb Minaev Can I call you, if I will have some troubles?
Because at this moment, I try do some unusual
But at this moment, it looks like has worked. Thanks
g
@Marshall Sorry, I am a little busy to take calls. You can write me in DM. But there are a lot more people here at #C0B8MA7FA that can help you.
m
Sure, no problem, I think, I have solution anyway, so it's OK 🙂
👍 1
j
I would rather recommend using
buildList
in this case:
Copy code
buildList {
    if (someString1 != null) add(someString1)
    add(someString2)
    if (someString3 != null) add(someString3)
}
👍 5
❤️ 1
plus1 2
m
Yeah, Python has some syntax like that
d
buildList
the most flexible way to do this.
s
Copy code
listOfNotNull(nullableString, someString2, someString3.takeIf { it.all(Char::isDigit) })
but
buildList {}
is probably a better option overall
d
If your list can legitimately contain nulls, buildList absolutely is the better option.
1